Writing XML attributes and namespace declarations in a specific order

前端 未结 1 1379
一生所求
一生所求 2020-12-12 04:27

I am trying to make an XML file with a root element:


So I have an element

相关标签:
1条回答
  • 2020-12-12 05:13

    XML attribute and namespace declaration order should never matter

    Attribute order is insignificant per the XML Recommendation:

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

    Namespace declaration are like attributes (W3C Namespaces in XML Recommendation, section 3 Declaring Namespaces),

    [Definition: A namespace (or more precisely, a namespace binding) is declared using a family of reserved attributes. Such an attribute's name must either be xmlns or begin xmlns:. These attributes, like any other XML attributes, may be provided directly or by default. ]

    with similarly insignificant ordering.

    So, no conformant XML tool or library will care about the order of XML attributes and XML namespace declarations, and neither should you.

    This is why XML libraries generally do not provide a way to constrain attribute ordering, and trying to do so is almost always a sign that you're doing something wrong.


    ...except for rarely needed normalization applications

    The XML recommendations will all consider attribute ordering and namespace declaration 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 an unavoidable need for attribute ordering. The need to establish lexical equality/inequality for digital signatures (XML Signature Syntax and Processing Version 1.1) is one such exception.

    See also (but only if you absolutely must order XML attributes and namespaces declarations):

    • .NET

      • Controlling the order of XML namepaces
      • How to specify the order of XmlAttributes, using XmlSerializer
      • .NET Serialization Ordering
    • Java

      • Order of XML attributes after DOM processing
      • java xml library that preserves attribute order
    • XSLT

      • XSLT to order attributes
    • Multi-platform

      • Saxon serialization parameters: saxon:attribute-order? and saxon:canonical?
    0 讨论(0)
提交回复
热议问题