本文介绍了以特定顺序编写XML属性和名称空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用根元素制作XML文件:

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

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

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

So I have an element Commanda namespace namespaceURIa prefix urnand finally an attribute string with name complete a value true and no namespace.

我为此做的代码返回:

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

所以问题是我希望属性字符串在XML文件中的名称空间定义之前,并且在此网站上找不到类似的问题.

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.

我尝试编写带前缀和名称空间的StartElement,然后编写不带名称空间的AttributeString,这将返回具有已定义名称空间的根元素,然后是属性字符串.我还尝试过只定义一个起始元素,然后定义两个属性字符串,但后来我找不到将前缀写入起始元素的方法.

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.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属性和命名空间声明顺序应该无关紧要

属性顺序微不足道 XML建议:

命名空间声明就像属性( XML推荐中的W3C命名空间,第 3声明命名空间),

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

具有类似的微不足道的顺序.

with similarly insignificant ordering.

因此,没有一致的XML工具或库将关心XML属性和XML名称空间声明的顺序,您也不应该关心.

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

这就是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建议都会将属性顺序和名称空间声明顺序视为无关紧要,但请参见 XML标准化建议,或者如果您的规范的XML建议应用程序不可避免地需要属性排序.需要为数字签名建立词汇相等/不相等( XML签名语法和处理版本1.1 )就是这样的例外.

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.

另请参见(但如果您绝对必须必须订购XML属性和名称空间声明,则请仅 )

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

  • .NET

  • .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

多平台

  • Saxon serialization parameters: saxon:attribute-order? and saxon:canonical?

这篇关于以特定顺序编写XML属性和名称空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:45