按特定顺序编写 XML 个属性和命名空间声明

Writing XML attributes and namespace declarations in a specific order

我正在尝试创建一个包含根元素的 XML 文件:

<urn:Command complete="true" xmlns:urn="namespaceURI">

所以我有一个元素Command 命名空间 namespaceURI 前缀 urn 最后是名称为 complete、值为 true 且没有名称空间的属性字符串。

我为此编写的代码returns:

<urn:Command xmlns:urn="namespaceURI" complete="true">

所以问题是我希望属性字符串在 XML 文件中的命名空间定义之前,我在这个网站上找不到类似的问题。

我试过写一个带有前缀和命名空间的 StartElement 然后写一个没有命名空间的 AttributeString,这个 returns 根元素首先是定义的命名空间,然后是属性细绳。 我也试过只定义一个开始元素,然后定义两个属性字符串,但是我找不到将前缀写入开始元素的方法。

这是我的原始代码,returns 具有命名空间定义的根元素首先是属性定义:

`Dim Writer as System.Xml.XmlWriter;
dim writerSettings  as System.Xml.XmlWriterSettings;
dim basePath as string;
dim source as string;
dim destination as string;

writerSettings = new System.Xml.XmlWriterSettings();
'writerSettings.ConformanceLevel= false;
'writerSettings.Encoding = new System.Text.UTF8Encoding(false);
writerSettings.OmitXmlDeclaration = false;

basePath = System.IO.Path.Combine("\wnlcuieb502\WEI\Outbound","RolexSet");
source  = System.IO.Path.Combine(basePath,"\wnlcuieb502\WEI\Outbound","TEST.XML");

Writer = System.Xml.XmlWriter.Create(source,writerSettings);
Writer.WriteStartDocument();

Writer.WriteStartElement("urn","SetPackagingOrder","urn:laetus.com:ws:tts:mes");
Writer.WriteAttributeString("complete",null,"true");
Writer.WriteEndElement();
Writer.WriteEndDocument();
Writer.dispose();


try
    destination = System.IO.Path.Combine(basePath,"TEST.XML");
    while not System.IO.File.Exists(destination)        
        System.IO.File.Move(source,destination);
    endwhile;
catch
    LogError(Me.HierarchicalName + ": Could not move XML file: "+ "TEST.XML" +" from " + source + " to " + destination + ", Error: " + error.Message);
endtry;`

XML 属性和命名空间声明顺序应该无关紧要

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.

命名空间声明就像属性(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. ]

排序同样无关紧要。

因此,没有符合规范的 XML 工具或库会关心 XML 属性和 XML 命名空间声明的顺序,而且两者都不会你应该吗

这就是为什么 XML 库通常不提供限制属性排序的方法,而尝试这样做几乎总是表明您做错了什么。


...除了很少需要的规范化应用程序

XML 建议都将属性顺序和名称空间声明顺序视为无关紧要,但请参阅 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) 就是这样的一个例外。

另请参阅(但是 如果您绝对必须 订购 XML 属性和名称空间声明):

  • .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
  • 多平台