2016-10-14 52 views
0

我有一個xsl:模板,它有兩種風格,具體取決於屬性中的值。我需要多次調用此模板,每次都使用不同的標籤進行過濾。如何調用具有多個條件的xsl:apply-templates

我有下面的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<document> 
    <item name="Recommendations"> 
     <richtext> 
      <pardef/> 
      <par><run>This is the </run><run>preamble.</run></par> 
      <pardef list='bullet'/> 
      <par><run>This is the </run><run>first bullet.</run></par> 
      <par><run>This is the second </run><run>bullet.</run></par> 
     </richtext> 
    </item> 
    <item name="Comments"> 
     <richtext> 
      <pardef/> 
      <par><run>This is the </run><run>preamble.</run></par> 
      <pardef list='bullet'/> 
      <par><run>This is the </run><run>first bullet.</run></par> 
      <par><run>This is the second </run><run>bullet.</run></par> 
     </richtext> 
    </item> 
</document> 

多虧了@ UL1發佈here的解決方案,我有以下XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

<xsl:output indent="yes"/> 
<xsl:key name="key-for-par" match="document/item/richtext/par" use="generate-id(preceding-sibling::pardef[1])"/> 

<xsl:template match="/"> 
<html> 
    <body> 
     <table> 
      <tr> 
       <td>Recommendations</td> 
       <td> 
        <xsl:apply-templates select="pardef[@list = 'bullet']"/> 
        <xsl:apply-templates select="pardef[not(@list)]"/> 
       </td> 
      </tr> 
      <tr> 
       <td>Comments</td> 
       <td> 
        <xsl:apply-templates select="pardef[@list = 'bullet']"/> 
        <xsl:apply-templates select="pardef[not(@list)]"/> 
       </td> 
      </tr> 
     </table> 
    </body> 
</html> 
</xsl:template> 

<xsl:template match="pardef[@list = 'bullet']"> 
    <ul> 
     <xsl:for-each select="key('key-for-par', generate-id(.))"> 
     <li> 
     <xsl:value-of select="run" separator=""/> 
     </li> 
     </xsl:for-each> 
    </ul> 
</xsl:template> 

    <xsl:template match="pardef[not(@list)]"> 
     <p> 
      <xsl:for-each select="key('key-for-par', generate-id(.))"> 
        <xsl:value-of select="run" separator=""/> 
      </xsl:for-each> 
     </p> 
    </xsl:template> 

</xsl:stylesheet> 

回答

0

我認爲最大的問題是,你做一個應用模板選擇pardef,但你的上下文是/

這是我會做什麼(這只是儘管因爲你沒有顯示您所需的輸出應該是什麼猜測)...

XML輸入

<document> 
    <item name="Recommendations"> 
     <richtext> 
      <pardef/> 
      <par><run>This is the </run><run>preamble.</run></par> 
      <pardef list='bullet'/> 
      <par><run>This is the </run><run>first bullet.</run></par> 
      <par><run>This is the second </run><run>bullet.</run></par> 
     </richtext> 
    </item> 
    <item name="Comments"> 
     <richtext> 
      <pardef/> 
      <par><run>This is the </run><run>preamble.</run></par> 
      <pardef list='bullet'/> 
      <par><run>This is the </run><run>first bullet.</run></par> 
      <par><run>This is the second </run><run>bullet.</run></par> 
     </richtext> 
    </item> 
</document> 

XSLT 2.0

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes"/> 

    <xsl:key name="key-for-par" match="par" 
    use="generate-id(preceding-sibling::pardef[1])"/> 

    <xsl:template match="/*"> 
    <html> 
     <body> 
     <table> 
      <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="pardef[@list = 'bullet']"> 
    <ul> 
     <xsl:for-each select="key('key-for-par', generate-id())"> 
     <li> 
      <xsl:value-of select="run" separator=""/> 
     </li> 
     </xsl:for-each> 
    </ul> 
    </xsl:template> 

    <xsl:template match="pardef[not(@list)]"> 
    <p> 
     <xsl:for-each select="key('key-for-par', generate-id())"> 
     <xsl:value-of select="run" separator=""/> 
     </xsl:for-each> 
    </p> 
    </xsl:template> 

    <xsl:template match="run"> 
    <!--"run" is being pulled. suppress output when pushed.--> 
    </xsl:template> 

</xsl:stylesheet> 

輸出

<html> 
    <body> 
     <table> 
     <tbody> 
      <tr> 
       <td>Recommendations</td> 
       <td> 
        <p>This is the preamble.</p> 
        <ul> 
        <li>This is the first bullet.</li> 
        <li>This is the second bullet.</li> 
        </ul> 
       </td> 
      </tr> 
      <tr> 
       <td>Comments</td> 
       <td> 
        <p>This is the preamble.</p> 
        <ul> 
        <li>This is the first bullet.</li> 
        <li>This is the second bullet.</li> 
        </ul> 
       </td> 
      </tr> 
     </tbody> 
     </table> 
    </body> 
</html> 
+0

非常感謝:) – b00kgrrl