XPath 1.0 UNION 中返回属性的顺序 [英] XPath 1.0 Order of returned attributes in a UNION

查看:24
本文介绍了XPath 1.0 UNION 中返回属性的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<merge>
    <text>
        <div begin="A"   end="B" />
        <div begin="C"   end="D" />
        <div begin="E"   end="F" />
        <div begin="G"   end="H" />
    </text>
</merge>

我需要一组 UNIONed 属性节点,顺序为 A、B、C、D、E、F、G、H,这将起作用:

I need a UNIONed set of attribute nodes, in the order A,B,C,D,E,F,G,H, and this will work:

/merge/text/div/@begin | /merge/text/div/@end

但仅当每个 @begin 出现在每个 @end 之前,因为 UNION 运算符被指定为按文档顺序返回节点.(是吗?)

but only if each @begin comes before each @end, since the UNION operator is spec'd to return nodes in document order. (Yes?)

我需要节点集的顺序相同,即使属性在文档中以不同的顺序出现,如下所示:

I need the nodeset to be in the same order, even if the attributes appear in a different order in the document, as here:

<merge>
    <text>
        <div end="B"   begin="A" />
        <div begin="C" end="D"   />
        <div end="F"   begin="E" />
        <div begin="G" end="H"   />
    </text>
</merge>

也就是说,我需要元素遵循文档顺序,但每个元素中的属性遵循确定的顺序(指定的或按属性名称的字母顺序).

That is, I need elements to follow document order, but the attributes in each element to follow a determined order (either specified or alphabetical by attribute name).

推荐答案

这在纯 XPath 中根本不可能.首先,XML 中的属性是无序的.来自 XML 1.0 建议:

This simply isn't possible in pure XPath. First of all, attributes in XML are unordered. From the XML 1.0 Recommendation:

请注意开始标签中属性规范的顺序或空元素标签不重要.

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

XPath 引擎可能会按照它们在文档中出现的顺序读取和存储它们,但就规范而言,这只是一个不能依赖的巧合.

An XPath engine might be reading and storing them in the order they appear in the document, but in terms of the spec, this is just a happy coincidence that cannot be relied upon.

其次,XPath 没有排序功能.因此,您最好的选择是在选择元素后以您的宿主语言(例如 XSLT 或通用 PL)对元素进行排序.

Second, XPath has no sorting functionality. So, your best option is to sort the elements in your host language (e.g. XSLT or a general-purpose PL) after they've been selected.

以下是在 XSLT 中按值对这些属性进行排序的方法:

Here's how to sort those attributes by value in XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:apply-templates
            select="/merge/text/div/@*[name()='begin' or name()='end']">
            <xsl:sort select="."/>
        </xsl:apply-templates>
    </xsl:template>
</xsl:stylesheet>

请注意,我还将您的两个表达式合并为一个.

Note that I also merged your two expressions into one.

使用以下内容按文档顺序输出开始/结束对(如注释中所述):

Use the following to output begin/end pairs in document order (as described in the comments):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="div">
        <xsl:value-of select="concat(@begin, @end)"/>
    </xsl:template>
</xsl:stylesheet>

这篇关于XPath 1.0 UNION 中返回属性的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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