2016-11-02 15 views
0

個別に動作する複数のテンプレートを含むXSLTスタイルシートを使用していますが、これは苦労しています。私はPARテンプレートまたはTABLEテンプレートを呼び出すことができますが、両方を呼び出すことはできません。どうすればそれをまとめることができますか?ここで複数のテンプレートを呼び出すXSLT

はXMLである:ここでは

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="AllTogether.xslt"?> 
<document> 
    <item name="Some bullets"> 
     <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> 
     </richtext> 
    </item> 
    <item name="A table"> 
     <richtext> 
      <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> 

は、所望の出力です:

<html> 
    <head/> 
    <body> 
    <table border="1"> 
     <tbody> 
      <tr> 
       <td>Some bullets</td> 
       <td> 
       <p>This is the <span style="text-decoration: underline;">preamble.</span></p><ul> 
       <li>This is the first bullet.</li></ul> 
       <p></p><ul> 
       <li>This is the second bullet.</li></ul> 
       <p>This is the conclusion.</p> 
       </td> 
      </tr> 
      <tr> 
       <td>A table</td> 
       <td> 
       <table border="1"> 
        <tr> 
         <td>Total Savings ($M)</td> 
         <td></td> 
         <td>0.9360</td> 
         <td>5.0047</td> 
         <td>8.8080</td> 
        </tr> 
       </table> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
    </body> 

そして、ここでは、スタイルシートです:

<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="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 = 'unordered'">  
        <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:value-of select="run" separator=""/> 
     </li> 
    </xsl:template> 

    <xsl:template match="par" mode="para"> 
     <p> 
      <xsl:value-of select="run" separator=""/> 
     </p> 
    </xsl:template> 

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

</xsl:stylesheet> 

答えて

1

問題がで発生しますこのテンプレートこの以内に電子...

<xsl:template match="richtext/par"> 

あなたは<xsl:for-each-group select="par"を行うが、あなたはすでにpar要素の上に配置されているように、これは何も存在しないそのうち、そのpar要素子を試してみて、選択します。

ソリューションはそうのようなrichtextに一致するようにテンプレートを変更することです:

<xsl:template match="richtext[par]"> 

あなたが唯一の子par要素を持っているrichtext要素にマッチしており、それがでrichtext要素とは一致しませんその方法子のtableであり、他のテンプレートを依然として一致させることができます。

関連する問題