XPath 1.0:将节点集中的第一项移动到最后一个位置
给定一个XML文档XPath 1.0:将节点集中的第一项移动到最后一个位置,xpath,xslt-1.0,Xpath,Xslt 1.0,给定一个XML文档 <items> <item><key></key><value>empty</value></item> <item><key>A</key><value>foo</value></item> <item><key>C</key><value>data</valu
<items>
<item><key></key><value>empty</value></item>
<item><key>A</key><value>foo</value></item>
<item><key>C</key><value>data</value></item>
<item><key>B</key><value>bar</value></item>
</items>
空的
阿福
Cdata
Bbar
给定/items/item节点集,我希望将第一个项目移动到最后一个位置,同时将所有其他项目保持在同一位置
无法使用的方法:
- |联合操作员保持文档顺序
我只想移动一个项目,而不是对整个项目列表进行排序
<items>
<item><key>A</key><value>foo</value></item>
<item><key>C</key><value>data</value></item>
<item><key>B</key><value>bar</value></item>
<item><key></key><value>empty</value></item>
</items>
阿福
Cdata
Bbar
空的
注意:要移动的项目可以通过第一个位置或空键来识别(如果有帮助)。一种方法是将以下模板与标识模板结合使用:
输出为:
<item>
<key>A</key>
<value>foo</value>
</item>
<item>
<key>C</key>
<value>data</value>
</item>
<item>
<key>B</key>
<value>bar</value>
</item>
<item>
<key/>
<value>empty</value>
</item>
A.
福
C
数据
B
酒吧
空的
添加标识模板使用
items
元素将其包围,以提供完整的所需输出。一种方法是将以下模板与标识模板结合使用:
输出为:
<item>
<key>A</key>
<value>foo</value>
</item>
<item>
<key>C</key>
<value>data</value>
</item>
<item>
<key>B</key>
<value>bar</value>
</item>
<item>
<key/>
<value>empty</value>
</item>
A.
福
C
数据
B
酒吧
空的
添加标识模板将使用items
元素包围该模板,以提供完整的、所需的输出。这可能会对您有所帮助
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items">
<xsl:copy>
<!-- Output attributes, if any. -->
<xsl:apply-templates select="@*"/>
<!-- Out item(s) that are not first. -->
<xsl:apply-templates select="item[position() != 1]"/>
<!-- Output the first item. -->
<xsl:apply-templates select="item[position() = 1]"/>
</xsl:copy>
</xsl:template>
这可能会对您有所帮助
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items">
<xsl:copy>
<!-- Output attributes, if any. -->
<xsl:apply-templates select="@*"/>
<!-- Out item(s) that are not first. -->
<xsl:apply-templates select="item[position() != 1]"/>
<!-- Output the first item. -->
<xsl:apply-templates select="item[position() = 1]"/>
</xsl:copy>
</xsl:template>
谢谢,这是对问题的严格回答,但不是幂等的(不能用相同的输出重复运行)。谢谢,这是对问题的严格回答,但不是幂等的(不能用相同的输出重复运行)。谢谢,这是我采取的方法,因为它也是幂等的。谢谢,这是我采用的方法,因为它也是幂等的。