使用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="reverse(*)"/>
</xsl:template>
嗯,对不起,这是为了彻底扭转他们,我可以看到你真的不想扭转一切。 在这种情况下,最简单的方法是在`select属性中手动编码顺序:
<xsl:template match="person">
  <xsl:apply-templates select="name[2]"/>
  <xsl:apply-templates select="id[2]"/>
  <xsl:apply-templates select="name[1]"/>
  <xsl:apply-templates select="id[1]"/>
   ...
</xsl:template>
(顺便说一句,这不是一个非常好的格式来存储你的数据,你应该将每个人都包裹在一个
<person>
标签中,就像一个接一个地写下它们然后摆弄订单是一个等待发生的事故。)     
如果你总是需要交换前2个人,那么你可以这样做:
<xsl:template match="person">
 <xsl:apply-templates select="name[position()=2]" />
 <xsl:apply-templates select="id[position()=2]" />

 <xsl:apply-templates select="name[position()=1]" />
 <xsl:apply-templates select="id[position()=1]" />

 <xsl:apply-templates select="node()[position() &gt; 4]" />
</xsl:template>
如果你为每个“名字”分别有
<person>
元素,这会更容易。 “id”对。     

要回复问题请先登录注册