XSLT 排序属性 [英] XSLT to order attributes

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

问题描述

我有一个输入 XML 文档:

I have an input XML document:

输入 XML

<?xml version="1.0" encoding="UTF-8"?>
<allNames id="ID_0" b:type="a:UnstructuredName">
    <typeName>KnownBy</typeName>
    <startDate>2001-01-01-05:00</startDate>
    <fullName>ABCD 004 COMPANY INC</fullName>
</allNames>

我需要应用 XSLT 将其转换为

I require to apply an XSLT to convert this to

输出 XML

<?xml version="1.0" encoding="UTF-8"?>
<allNames  b:type="a:UnstructuredName"  id="ID_0">
    <typeName>KnownBy</typeName>
    <startDate>2001-01-01-05:00</startDate>
    <fullName>ABCD 004 COMPANY INC</fullName>
</allNames>

唯一的变化是 allNames 元素中的属性顺序变化.我查了另一篇文章,写了 XSLT 来对属性进行排序,但我不知道如何让整个事情正常工作.

The only change is the attribute ordering change in the allNames element. I looked up another post and wrote XSLT that orders attributes, but I do not know how to get the whole thing working.

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" indent="yes"/>
    <xsl:variable name="attributes" select="document('mytest.xml')//attribute"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:variable name="self" select="."/>
        <xsl:for-each select="$attributes">
            <xsl:apply-templates select="$self/@*[name()=current()]"/>
        </xsl:for-each>   
     </xsl:template>
</xsl:stylesheet>

mytest.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <attributes>
        <attribute>b:type</attribute>
        <attribute>id</attribute>
    </attributes>

推荐答案

属性顺序无关紧要 根据 XML 推荐:

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

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

因此,XSLT 无法控制输出的属性顺序.

一般来说,XML 建议都会认为属性排序无关紧要,但请参阅 XML 规范化建议规范 XML 推荐 如果您的应用程序需要属性排序.但是,您必须在标准 XSLT 之外执行此操作.

In general, the XML recommendations will all consider attribute ordering to be insignificant, but see the section on attribute processing in the XML Normalization Recommendation or the Canonical XML Recommendation if your application has a need for attribute ordering. You'd have to do this outside of standard XSLT, however.

如果您认识到对 XML 属性强加排序本质上是有缺陷的,与互操作性背道而驰,并且完全超出了 XML 建议书和使用 XML 的既定实践,但您仍然 必须控制属性排序,这里有一些实现这种控制的方法...

If you recognize that imposing an ordering on XML attributes is intrinsically flawed, is contrary to interoperability, and is completely outside of both the XML Recommendation and established practices for working with XML, and yet you still must control attribute ordering, here are some ways of implementing such control...

正如 Michael Kay 在对这个问题的另一个回答中提到的那样,Saxon 9.5(PE 或更高版本)有一个 XSLT 扩展,可以控制序列化程序的属性排序.请参阅 saxon:attribute-order 了解详情.

As Michael Kay mentions in another answer to this question, Saxon 9.5 (PE or higher) has an XSLT extension that provides control over the serializer's ordering of attributes. See saxon:attribute-order for details.

您可以对标准 XSLT 生成的 XML 进行后处理.在 XML 库级别下操作,您当然可以通过字符或字符串级别处理获得对属性排序的完全词汇控制.

You could post-process the XML produced by standard XSLT. Operating beneath the XML library level, you can of course gain full lexical control of attribute ordering via character or string level processing.

您可以依赖 XML 库提供的排序的实现细节.例如,一些库会根据属性名称按字母顺序写出属性,或者会保留提供给它们的属性顺序.显然,依赖于实现细节本质上是不可靠的.也就是说,例如 XMLStreamWriter.writeAttribute 将继续兑现未来赋予它们的属性顺序.

You could rely upon implementational details of the ordering provided by an XML library. For example, some libraries will write attributes out in alphabetical order according to the names of the attributes, or will preserve the attribute order provided to them. Obviously, relying upon implementational details is inherently unreliable. That said, it is likely that implementations of, for example, XMLStreamWriter.writeAttribute will continue to honor the order of attributes given to them in the future.

最后一次重申所有 XML 属性排序问题的真正答案是在结束之前按顺序...

One last reiteration of the real answer to all questions of XML attribute ordering is in order before closing...

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

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