我有一個這樣的XML:XSLT 2.0 - 遍歷節點集的變量,但需要處理在循環的其他元素以及
<?xml version="1.0" encoding="UTF-8"?>
<nodes>
<n c="value2"/>
<n>Has a relation to node with value2</n>
<n>Has a relation to node with value2</n>
<n c="value"/>
<n>Has a relation to node with value</n>
<n c="value1"/>
<n>Has a relation to node with value1</n>
</nodes>
我排序所有有屬性在可變元素,那麼我迭代在的這個變量中,對於每個循環。但是在每個循環結束時,我需要打印當前處理元素下面的元素的值(原始XML),並且沒有混亂。
這意味着:在沒有屬性的<n>
上調用apply-templates,但是「select」attr。在apply-templates
不起作用,可能是因爲我現在在變量循環。
有沒有解決方案? 感謝
這裏是XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="nodes">
<xsl:variable name="sorted">
<xsl:for-each select="n[@c]">
<xsl:sort select="@c"></xsl:sort>
<xsl:copy-of select="."></xsl:copy-of>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$sorted/n">
<xsl:value-of select="@c"></xsl:value-of>
<xsl:apply-templates select="/nodes/n[2]"></xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="n[not(@c)]">
<xsl:value-of select="."></xsl:value-of>
</xsl:template>
</xsl:stylesheet>
這僅僅是例子,這一切都是大項目:)
所需的輸出具有更復雜的XPath(現在連一個簡單的一部分不起作用):
Value
Has a relation to node with value
Value1
Has a relation to node with value1
Value2
Has a relation to node with value2
Has a relation to node with value2
現在有點清楚了嗎?
謝謝你,我會改進我原來的例子,使它更有意義。 – MartinM
完美的謝謝,我只是試圖用更復雜的方式。對於每個組是一個很好的解決方案... – MartinM