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

查看:178
本文介绍了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?

先谢谢了.

UPDATE ----------------------

我尝试了看起来不错并且看起来很简单的Andyb解决方案,但是该解决方案还有另一个问题.

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

假设我有此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.

推荐答案

您可以使用多个 说明,例如:

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>

,由于默认的数据类型为文本",默认的 order 为升序",因此可以得到所需的输出.

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不同(<c></c>处于不同位置).

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天全站免登陆