具有递归和条件的XSLT模板

时间:2016-02-26 08:06:19

标签: xslt

我是XSLT的新手,需要帮助

这是我的XML源文件:

<cars best-category="83">
    <category id="1" name="SUV">
        <category id = "50" name="BMW">
        </category>
    </category>
    <category id="2" name="combi">
        <category id = "60" name="Volkswagen">
           <category id="102" name="4x4">
           </category>      
        </category>                     
        <category id = "83" name="Skoda">           
        </category>
        <category id = "32" name="Seat">            
        </category>
    </category>
    <category id="12" name="another">
    </category>
</cars>

我需要这个结果(按最佳类别值选择全名属性路径):

<CATEGORYTEXT>combi | Skoda</CATEGORYTEXT>

我的XSLT:

       <xsl:for-each select="cars/category">              
          <CATEGORYTEXT>              
            <xsl:apply-templates select="category" >
              <xsl:with-param name="text" >
                <xsl:value-of select="@name" />
              </xsl:with-param>
            </xsl:apply-templates>              
          </CATEGORYTEXT>
        </xsl:for-each

和我的模板:

<xsl:template match="category">
            <xsl:param name="text" />
            <xsl:if test="category">
              <xsl:apply-templates select="category">
                <xsl:with-param name="text">
                  <xsl:value-of select="$text" />
                  <xsl:text>&#xA;|&#xA;</xsl:text>
                  <xsl:value-of select="@name" />
                </xsl:with-param>
              </xsl:apply-templates>
            </xsl:if>
            <xsl:value-of select="$text" />
            <xsl:text>&#xA;|&#xA;</xsl:text>
            <xsl:value-of select="@name" />    
        </xsl:template>
芽我不知道,如何通过最佳类别价值筛选结果......任何人都可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

怎么样:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="cars">
    <xsl:apply-templates select=".//category[@id=current()/@best-category]"/>
</xsl:template>

<xsl:template match="category">
    <CATEGORYTEXT>
        <xsl:for-each select="ancestor::category">
            <xsl:value-of select="@name" />
            <xsl:text> | </xsl:text>
        </xsl:for-each>
        <xsl:value-of select="@name" />
    </CATEGORYTEXT>
</xsl:template>

</xsl:stylesheet>