如何使用 XSLT 反转 XML 数据标签? [英] How to reverse XML data tags using XSLT?

查看:50
本文介绍了如何使用 XSLT 反转 XML 数据标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,下面的 XML 文件.

For example, below XML file.

<person>
    <name>John</name>
    <id>1</id>
    <name>Diane</name>
    <id>2</id>
    <name>Chris</name>
    <id>3</id>
</person>

现在,在 XSLT 中,如果我编码:

Now, In XSLT, If I code:

<xsl:template match="person">
 <xsl:apply-templates/>
</xsl:template>

因此,在 HTML 文件中,它将显示 John1Diane2Chris3.

So, In HTML file It will display John1Diane2Chris3.

但是,我需要以下输出:Diane2John1Chris3

But, I need following output: Diane2John1Chris3

我需要颠倒前 2 个数据标签的顺序.下面是前 2 个标签

I need to reverse order of first 2 data tags. Here below first 2 tags

<name>John</name>
<id>1</id>

<name>Diane</name>
<id>2</id>

大家有什么想法吗?

推荐答案

这是一个非常具体的问题的非常具体的解决方案:

Here's a very specific solution to a very specific problem:

<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

更一般的解决方案需要对问题进行更一般的描述.

A more general solution would require a more general description of the problem.

这篇关于如何使用 XSLT 反转 XML 数据标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆