如何使用XSLT反转XML数据标签?

例如,在XML文件下面。
<person>
    <name>John</name>
    <id>1</id>
    <name>Diane</name>
    <id>2</id>
    <name>Chris</name>
    <id>3</id>
</person>
现在, 在XSLT中,如果我编码:
<xsl:template match="person">
 <xsl:apply-templates/>
</xsl:template>
因此,在HTML文件中它将显示John1Diane2Chris3。 但, 我需要以下输出: Diane2John1Chris3 我需要反转前2个数据标签的顺序。 这里有前2个标签
<name>John</name>
<id>1</id>

<name>Diane</name>
<id>2</id>
任何想法的人?     
已邀请:
<xsl:template match="person">
  <xsl:apply-templates select="name[2]|id[2]"/>
  <xsl:apply-templates select="name[position() != 2]|id[position() != 2]"/>
</xsl:template>
这假设总是有一个
name
id
对。如果情况并非如此,解决方案将更加复杂。     
这是针对特定问题的非常具体的解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="person">
        <xsl:apply-templates select="name[text()='Diane']|id[text()='2']" />
        <xsl:apply-templates select="name[not(text()='Diane')] |
                                       id[not(text()='2')]" />
    </xsl:template>
</xsl:stylesheet>
输出:
Diane2John1Chris3
更通用的解决方案需要对问题进行更一般的描述。     
下面的代码将允许您控制要反转的第一个标签的数量,但我倾向于同意lwburk,如果您确定所有需要的只是仅反转两个第一个标签,那么它可能会有点过分。
<xsl:template match="person">
         <xsl:for-each select="name[position() &lt; 3]">
             <xsl:sort select="position()" data-type="number" order="descending"/>
             <xsl:apply-templates select="."/>
             <xsl:apply-templates select="./following-sibling::id[position() = 1]"/>
         </xsl:for-each>
         <xsl:apply-templates select="name[position() = 2]/following-sibling::*[position() &gt; 1]"/>
</xsl:template>
    

要回复问题请先登录注册