XSLT 1.0:将元素移动到父元素的底部并对它们进行排序

时间:2017-02-03 14:01:18

标签: xml xslt xslt-1.0

我想将一些元素移动到父元素的底部,并通过属性“messageid”

对它们进行排序

这是XML

<Root>
<parent>
<child/>
<child2 messageid="8"/>
<child/>
<child2 messageid="5"/>
<child/>
<child2 messageid="7"/>
</parent>
</Root>

这是想要的XML输出

<Root>
<parent>
<child/>
<child/>
<child/>
<child2 messageid="5"/>
<child2 messageid="7"/>
<child2 messageid="8"/>
</parent>
</Root>

我想我需要使用xsl:copy但我不知道该怎么做。谢谢你的帮助

2 个答案:

答案 0 :(得分:1)

这样的事情应该有效:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="parent">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates select="*[not(self::child2)]" />
      <xsl:apply-templates select="child2">
        <xsl:sort data-type="number" select="@messageid" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*|node()">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates select="*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:1)

可能更容易 在AtomicInteger节点上使用略微修改的身份模板似乎可以完成这项任务:

parent