简体   繁体   English

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

[英]Writing XML attributes and namespace declarations in a specific order

I am trying to make an XML file with a root element:我正在尝试使用根元素创建一个 XML 文件:

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

So I have an element Command a namespace namespaceURI a prefix urn and finally an attribute string with name complete a value true and no namespace.所以我有一个元素Command a namespace namespaceURI前缀urn和最后一个带有 name 的属性字符串complete一个值true并且没有命名空间。

The code I have made to do this returns:我为此所做的代码返回:

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

So the problem is I would like the attribute string to be before the namespace definition in the XML file and I can't find a similar problem on this website.所以问题是我希望属性字符串在 XML 文件中的命名空间定义之前,我在这个网站上找不到类似的问题。

I have tried writing a StartElement with a prefix and namespace then writing an AttributeString with no namespace, this returns the root element with the defined namespace first followed by the attribute string.我尝试编写一个带有前缀和名称空间的StartElement ,然后编写一个没有名称空间的AttributeString ,这将首先返回具有定义名称空间的根元素,然后是属性字符串。 I have also tried only defining a start element and then two attribute strings but then I can't find a way to write the prefix to the start element.我也尝试过只定义一个开始元素,然后定义两个属性字符串,但是我找不到将前缀写入开始元素的方法。

This is my original code that returns the root element with a namespace definition first the the attribute definition:这是我的原始代码,它首先返回具有命名空间定义的根元素和属性定义:

`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 and namespace declaration order should never matter XML 属性和命名空间声明顺序应该无关紧要

Attribute order is insignificant per the XML Recommendation :根据XML 建议,属性顺序无关紧要

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 ),命名空间声明就像属性( XML 建议中的 W3C 命名空间,第3声明命名空间),

[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: .此类属性的名称必须是xmlns或以xmlns:开头。 These attributes, like any other XML attributes, may be provided directly or by default .这些属性与任何其他 XML 属性一样,可以直接提供或默认提供 ] ]

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.因此,没有任何符合标准的 XML 工具或库会关心 XML 属性和 XML 命名空间声明的顺序,您也不应该关心。

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.这就是为什么 XML 库通常不提供约束属性排序的方法,而尝试这样做几乎总是表明您做错了什么。


...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. XML 建议都认为属性排序和命名空间声明排序无关紧要,但如果您的应用程序不可避免地需要属性排序,请参阅XML 规范化建议规范 XML 建议中有关属性处理部分 The need to establish lexical equality/inequality for digital signatures ( XML Signature Syntax and Processing Version 1.1 ) is one such exception.需要为数字签名( XML 签名语法和处理版本 1.1 )建立词法相等/不等式就是这样的一个例外。

See also (but only if you absolutely must order XML attributes and namespaces declarations):另请参阅(但当您绝对必须对XML 属性和名称空间声明进行排序时):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM