如何使用多个条件调用xsl:apply-templates

时间:2016-10-14 14:24:29

标签: xml xslt

我有一个xsl:template,它有两种风格,具体取决于属性中的值。我需要多次调用此模板,每次都在不同的标签上进行过滤。

我有以下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>

1 个答案:

答案 0 :(得分: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>