问题
Similar questions were asked by others before, and I understand that the order of XML attributes cannot be specified in XML or schema. However, I would like to know, whether the order of XML attributes in WSDL are randomly generated by WCF or they follow some rules.
For example, at one stage we had the following (part of) XML tag in generated WSDL.
<xs:element maxOccurs="unbounded" minOccurs="0"
Now generating it again it becomes
<xs:element minOccurs="0" maxOccurs="unbounded"
with the order of min and max swapped.
Another example:
<wsdl:port binding="tns:BasicHttpBinding_IReferenceDataBusinessService" name="BasicHttpBinding_IReferenceDataBusinessService">
becomes
<wsdl:port name="BasicHttpBinding_IReferenceDataBusinessService" binding="tns:BasicHttpBinding_IReferenceDataBusinessService">
Just wondering what decides such orders in WCF? Is it possible to have the process adjusted to keep the same orders to make comparing two WSDL files easier?
Thank you.
回答1:
You acknowledge that attribute order is insignificant and then go on to imply that you wish to act on attribute order anyway. By definition, it's an implementation detail. Rely on any given attribute ordering at your own peril.
Furthermore, attribute order is merely one of many obstacles to comparing XML documents lexically. Before you embark on such an endeavour, be familiar with everything covered by Canonical XML Version 1.1. Here's a summary:
The canonical form of an XML document is physical representation of the document produced by the method described in this specification. The changes are summarized in the following list:
- The document is encoded in UTF-8
- Line breaks normalized to #xA on input, before parsing
- Attribute values are normalized, as if by a validating processor
- Character and parsed entity references are replaced
- CDATA sections are replaced with their character content
- The XML declaration and document type declaration are removed
- Empty elements are converted to start-end tag pairs
- Whitespace outside of the document element and within start and end tags is normalized
- All whitespace in character content is retained (excluding characters removed during line feed normalization)
- Attribute value delimiters are set to quotation marks (double quotes)
- Special characters in attribute values and character content are replaced by character references
- Superfluous namespace declarations are removed from each element
- Default attributes are added to each element
- Fixup of xml:base attributes [C14N-Issues] is performed
- Lexicographic order is imposed on the namespace declarations and attributes of each element
Robust code doesn't rely on an implementation's accidental (non-essential) properties along any of these dimensions.
回答2:
At the end I decided to use Xml Sorter (https://xmlsorter.codeplex.com/). WSDL XML files are sorted before comparing, this way I could immediately spot the difference between two WSDL files in BeyondCompare and alike, rather than slowly scrolling down and trying to spot real differences from hundreds of differences (due to changed XML attribute orders).
来源:https://stackoverflow.com/questions/31195817/what-decides-the-order-of-xml-attributes-in-wcf-wsdl