2013-07-26 37 views
1

我的XML有点类似于下面显示的内容。我必须找到独特的类别。 XSLT 2.0中有许多简单的方法。但是,我不得不坚持到1.0 :(。几个斗争后,我找到了解决办法,我想分享。可能会帮助别人,请提高我的答案。我很欣赏。XSLT 1.0如何获取不同的值

<root> 
    <category> 
    this is Games category 
    </category> 
    <category> 
    this is Books category 
    </category> 
    <category> 
    this is Food category 
    </category> 
    <category> 
    this is Games category 
    </category> 
    <category> 
    this is Books category 
    </category> 
    <category> 
    this is Food category 
    </category> 
    <category> 
    this is Travel category 
    </category> 
    <category> 
    this is Travel category 
    </category> 
</root> 

解决方案。我在添加答案部分。谢谢。

回答

0

解决方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:variable name="test"> 
      <xsl:call-template name="delimitedvalues"> 
       <xsl:with-param name="paramvalues" select="//category" /> 
      </xsl:call-template> 
     </xsl:variable> 

     <xsl:call-template name="distinctvalues"> 
      <xsl:with-param name="values" select="$test" /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="distinctvalues"> 
     <xsl:param name="values"/> 
     <xsl:variable name="firstvalue" select="substring-before($values, ',')"/> 
     <xsl:variable name="restofvalue" select="substring-after($values, ',')"/> 
     <xsl:if test="contains($values, ',') = false"> 
      <xsl:value-of select="$values"/> 
     </xsl:if> 
     <xsl:if test="contains($restofvalue, $firstvalue) = false"> 
      <xsl:value-of select="$firstvalue"/> 
      <xsl:text>,</xsl:text> 
     </xsl:if> 
     <xsl:if test="$restofvalue != ''"> 
      <xsl:call-template name="distinctvalues"> 
       <xsl:with-param name="values" select="$restofvalue" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template name="delimitedvalues"> 
     <xsl:param name="paramvalues" /> 
     <xsl:value-of select="substring-before(substring-after($paramvalues,'this is '),' category')"/> 
     <xsl:if test="$paramvalues/following::category"> 
      <xsl:text>,</xsl:text> 
      <xsl:call-template name="delimitedvalues"> 
       <xsl:with-param name="paramvalues" select="$paramvalues/following::category" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

输出

Games,Books,Food,Travel 

源代码

http://www.xsltcake.com/slices/0iWpyI