XSLT 按标签名和属性值排序 [英] XSLT sort by tag name and attribute value

查看:38
本文介绍了XSLT 按标签名和属性值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的菜鸟,所以请原谅我的无知...我正在尝试按属性值和标记名称对一个简单的 XML 文件进行排序,但在访问属性值时遇到了困难.这是一个完整的例子:

I'm a noob with XSLT, so please excuse my ignorance... I'm trying to sort a simple XML file by attribute value and tag name, but I struggle in accessing the value of the attribute. Here is a complete example:

<a>
    <b attribute="e"></b>
    <b attribute="b"></b>
    <d attribute="a"></d>
    <c></c>
</a>

而预期的结果是:

<a>
    <b attribute="b"></b>
    <b attribute="e"></b>
    <c></c>
    <d attribute="a"></d>
</a>

这是我尝试解决此问题的方法:

Here is my attempt to solve this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />
    <xsl:strip-space elements="*" />

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

            <xsl:apply-templates select="node()">
                <xsl:sort select="name()"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

这显然根本行不通...

And this obviously don't work at all...

在上面的例子中,我想按属性值对 b 标签进行排序,但正如你所看到的,d 标签没有按属性值排序,因为它是另一个标签名字...

In the above example I want to sort the b tag by their attribute value but as you can see the d tag is not sorted by attribute value because it's another tag name...

我想知道这是否可以使用 XSLT...你有什么想法吗?

I wonder if this is possible using XSLT... Do you have an idea?

提前致谢.

更新----------------------

我尝试了似乎工作正常且看起来很简单的 andyb 解决方案,但我对此解决方案还有另一个问题.

I tried andyb solution that seems to work fine and looks pretty simple, but I have another issue with this solution.

假设我有这个 XML:

Let's say I have this XML:

<a>
    <b attribute="e" optionalAttr="fg"></b>
    <b attribute="b"></b>
    <d attribute="a"></d>
    <c></c>
</a>

我为 b 标签添加了一个可选参数.应用andyb解决方案,可选参数将被忽略,因为它在模板中不匹配.结果如下:

I added an optional parameter for the b tag. Applying andyb solution the optional parameter will be ignored, because it is not matched in the template. Here is the result:

<a>
    <b attribute="b"></b>
    <b attribute="e"></b>
    <c></c>
    <d attribute="a"></d>
</a>

而不是我期望的以下内容:

Instead of the following which is what I expect:

<a>
    <b attribute="b"></b>
    <b attribute="e" optionalAttr="fg"></b>
    <c></c>
    <d attribute="a"></d>
</a>

你有什么想法吗?提前致谢.

Do you have any idea? Thanks in advance.

推荐答案

您可以使用多个 xsl:sort 指令,例如:

You can use multiple xsl:sort instructions, for example:

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

并且由于默认的数据类型是文本"并且默认的顺序是升序",这给出了所需的输出.

and since the default data-type is "text" and the default order is "ascending" this gives the desired output.

编辑

这很奇怪,因为对于以下 XML:

This is strange, because for the following XML:

<a>
    <b attribute="e" optionalAttr="fg"></b>
    <b attribute="b"></b>
    <d attribute="a"></d>
    <c></c>
</a>

和上面的XSL,我得到这个结果:

and the XSL above, I get this result:

<a>
    <b attribute="b"></b>
    <b attribute="e" optionalAttr="fg"></b>
    <c></c>
    <d attribute="a"></d>
</a>

这包括所需的可选属性,但顺序与编辑的问题中的 XML 不同( 位于不同的位置).

This includes the desired optional attribute but the order is different to the XML in the edited question (<c></c> is in a different position).

这篇关于XSLT 按标签名和属性值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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