XSLT如何根据原始字段派生变量进行排序?

时间:2012-07-06 23:14:11

标签: xml xslt-1.0

我是xslt的新手,想知道如何基于派生变量进行排序,如下所示(使用xslt 1.0):

的xml:

<channel>
  <item>
     <title>#2: Second Guy</title>
  </item>
  <item>
     <title>#3: Third Guy</title>
  </item>
  <item>
     <title>#1: First Guy</title>
  </item>
</channel>

xslt尝试:

<xsl:for-each select="channel/item" >

        <xsl:sort select="$rank" data-type="number" order="ascending" />

        <xsl:variable name="rankStartPosn" select="string-length(substring-before(title, '#'))+1"/>
        <xsl:variable name="rankEndPosn" select="string-length(substring-before(title, ':'))+1"/>
        <xsl:variable name="rank" select="substring(title,number($rankStartPosn), number($rankEndPosn)-number($rankStartPosn))"/>

                <p class="Normal">
                    <xsl:value-of select="title" />
                </p>
            </xsl:if>
        </xsl:if>
    </xsl:for-each>

期望的输出:

#1: First Guy
#2: Second Guy
#3: Third Guy

提前感谢您的帮助! 乍得

2 个答案:

答案 0 :(得分:0)

啊......顿悟!

只是不要使用变量并将整个表达式放在sort的select ...中:

<xsl:sort data-type="number" order="ascending" 
             select="substring(title,number(string-length(substring-before(title, '#'))+2), number(string-length(substring-before(title, ':'))+1)-number(string-length(substring-before(title, '#'))+2))"/>

答案 1 :(得分:0)

<强>简单

<xsl:sort select="substring(substring-before(title, ':'), 2)" 
          data-type="number" order="ascending" />