2016-11-03 8 views
0

XSLTスタイルシートを実行すると、あいまいなルールの一致の警告が表示され、テンプレートの1つが呼び出されません。 richtext [par]の一致基準を持つテンプレートが呼び出されますが、richtext [table]の一致基準を持つテンプレートは一致しません。両方のテンプレートを起動して警告メッセージを表示させない方法ここでXSLTの曖昧なルールの一致の警告

がXMLである:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <item name="The Item"> 
     <richtext> 
      <pardef/> 
      <par def="20"> 
       <run>This is the </run> 
       <run><font style="underline"/>preamble.</run> 
      </par> 
      <pardef id="21" list="bullet"/> 
      <par def="21"> 
       <run>This is the </run> 
       <run>first bullet.</run> 
      </par> 
      <par def="20"> 
       <run/> 
      </par> 
      <par def="21"> 
       <run>This is the second </run> 
       <run>bullet.</run> 
      </par> 
      <par def="20"> 
       <run>This is the </run> 
       <run>conclusion.</run> 
      </par> 
      <table> 
       <tablerow> 
        <tablecell> 
         <par def="43"><run>Total Savings ($M)</run></par></tablecell> 
        <tablecell> 
         <pardef/> 
         <par def="50"><run></run></par></tablecell> 
        <tablecell> 
         <par def="44"><run>0.9360</run></par></tablecell> 
        <tablecell> 
         <par def="45"><run>5.0047</run></par></tablecell> 
        <tablecell> 
         <par def="46"><run>8.8080</run></par></tablecell></tablerow></table> 

     </richtext> 
    </item> 
</document> 

そして、ここではXSLTです:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output indent="yes" method="html"/> 
    <xsl:template match="/*"> 
     <html> 
      <body> 
       <table border="1"> 
        <tbody> 
         <xsl:apply-templates/> 
        </tbody> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="item"> 
     <tr> 
      <td><xsl:value-of select="@name"/></td> 
      <td> 
       <xsl:apply-templates/> 
      </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="richtext[table]"> 
     <table border="1"> 
      <xsl:for-each select="tablerow"> 
       <tr> 

        <xsl:for-each select="tablecell"> 
         <td> 
          <xsl:apply-templates /> 
         </td> 
        </xsl:for-each> 


       </tr> 
      </xsl:for-each> 

     </table>  
    </xsl:template> 

    <xsl:template match="richtext[par]"> 
     <xsl:for-each-group select="par[run[normalize-space()]]" group-adjacent="if (@def) then @def else preceding-sibling::par[run[normalize-space()]][@def][1]/@def"> 
      <xsl:variable name="listType" select="preceding-sibling::*[1][self::pardef]/@list" /> 
      <xsl:choose> 
       <xsl:when test="$listType = 'bullet'">  
        <ul> 
         <xsl:apply-templates select="current-group()" mode="list"/> 
        </ul> 
       </xsl:when> 
       <xsl:when test="$listType = 'ordered'">  
        <ol> 
         <xsl:apply-templates select="current-group()" mode="list"/> 
        </ol> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:apply-templates select="current-group()" mode="para" /> 
       </xsl:otherwise>  
      </xsl:choose> 
     </xsl:for-each-group> 
    </xsl:template> 


    <xsl:template match="par" mode="list"> 
     <li> 
      <xsl:apply-templates select="run" /> 
     </li> 
    </xsl:template> 

    <xsl:template match="par" mode="para"> 
     <p> 
      <xsl:apply-templates select="run" /> 
     </p> 
    </xsl:template> 



    <xsl:template match="run"> 
     <xsl:choose> 
      <xsl:when test="font[@style = 'underline']"> 
       <span style="text-decoration: underline;"> 
        <xsl:value-of select="text()" separator=""/> 
       </span> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="text()" separator=""/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

</xsl:stylesheet> 
+0

あなたのテンプレート 'richtext [table]'を見てください。コンテキストノードは "richtext"ですが、要素 "table"を作成します。だからあなたは 'richtext'をテーブルに置き換えます! [for-eachは何も実行せず、ノード上でxpathが一致しません。たぶんその "リッチテキスト/テーブル"に変更することによって解決? – uL1

答えて

2

現在のコードはrichtextどちらか持ってpar要素、またはtableない、またはその両方を想定しています。両方がある場合は、両方のテンプレートが同じ優先度で一致します。これはエラーとみなされます(XSLTはエラーのフラグを立てるか、最後のテンプレートを選択します)。それらを両方mode属性を与えるが、それらが存在するtableかどうかpar(あるいはその両方)に基づいて、関連するテンプレートを適用することrichtextに一致する単一のテンプレートを持っている必要がありますする

一つの方法:

また
<xsl:template match="richtext"> 
    <xsl:if test="par"> 
     <xsl:apply-templates select="." mode="par" /> 
    </xsl:if> 
    <xsl:if test="table"> 
     <xsl:apply-templates select="." mode="table" /> 
    </xsl:if> 
</xsl:template> 

<xsl:template match="richtext" mode="par"> 
    <xsl:for-each-group select="par" ...> 
     <!-- Current code --> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="richtext" mode="table"> 
    <table border="1"> 
     <xsl:for-each select="table/tablerow"> 
      <!-- Current code --> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

、あなたが現在代わりに、テーブルに一致するようにrichtext[table]に一致するテンプレートを変更することができ、その後、あなたはこの

<xsl:template match="richtext[par]"> 
    <xsl:for-each-group select="par" ...> 
     <!-- Current code --> 
    </xsl:for-each-group> 
    <xsl:apply-templates select="table" /> 
</xsl:template> 

<xsl:template match="richtext/table"> 
    <table border="1"> 
     <xsl:for-each select="tablerow"> 
      <!-- Current code --> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

でのような何かを行うことができますの場合richtextparがない場合はXSLTのビルトインテンプレートが適用され、table要素が選択されます。

table要素に配置されているため、内部xsl:for-eachを調整して、代わりにtablerowを選択する必要があります。実際に

、あなたもこのような何かを行うことができます:

<xsl:template match="richtext"> 
    <xsl:for-each-group select="par" ...> 
     <!-- Current code --> 
    </xsl:for-each-group> 
    <xsl:apply-templates select="table" /> 
</xsl:template> 

<xsl:template match="richtext/table"> 
    <table border="1"> 
     <xsl:for-each select="tablerow"> 
      <!-- Current code --> 
     </xsl:for-each> 
    </table> 
</xsl:template> 

をので、NOの場合にはparxsl:for-each-groupはとにかく何もしません。

+0

私は両方のオプションを試しましたが、テーブルテンプレートが呼び出されましたが、空のテーブルが作成されています。それは各テーブルセルにPAR要素が入っているからですか? – b00kgrrl

+0

あなたが使ったオプション(私は実際に3つのオプションを与えました;))に応じて、 'richtext'要素か' table'要素のどちらに置かれているかによって 'xsl:for-each'にselect節を調整する必要があります。私はこれをより明確にするために私の答えを修正しました。 –

+0

ありがとう! :) – b00kgrrl