使用:
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
下面是一個完整的例子。這種轉變:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<t>
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
</t>
</xsl:template>
<xsl:template match="node[not(val1 = val2)]">
<div>
<node>
<xsl:apply-templates/>
</node>
</div>
</xsl:template>
</xsl:stylesheet>
當這個XML文檔施加:
<t>
<node>
<val1>1</val1>
<val2>2</val2>
</node>
<node>
<val1>3</val1>
<val2>3</val2>
</node>
</t>
產生想要的,正確的結果:
<t>
<node>
<val1>3</val1>
<val2>3</val2>
</node>
<div>
<node>
<val1>1</val1>
<val2>2</val2>
</node>
</div>
</t>
解決的說明:
只要<xsl:apply-templates>
具有<xsl:sort>
孩子,就會根據<xsl:sort>
孩子(ren)中提供的數據對所選節點進行排序,並且在每個選定節點上應用模板的結果以該(排序)順序顯示在輸出中 - 不按文件順序。
在上面,我們的改造有:
<xsl:apply-templates select="node">
<xsl:sort select="not(val1 = val2)"/>
</xsl:apply-templates>
這意味着應用模板名爲node
的元素,它是真實的val1=val2
將應用模板來命名的元素的結果之前出現的結果node
其中val1=val2
不正確。這是因爲false
在true
之前排序。
如果這個解釋不明確,讀者可以閱讀更多有關<xsl:sort>
的文章。
一個很好的問題(+1)!查看我的答案,找到只需要發佈一次的簡短解決方案。 :) –
2010-05-18 13:13:55
@Dimitre Novatchev,我一如既往,任務設定不佳。查看有問題的更新。 – Kalinin 2010-05-18 13:26:04
我沒有看到任何更新! ??? – 2010-05-18 13:28:47