问题
What (if any) is the difference between the below xml snippets, with regards to namespace?
Form1
<prf:XmlElement1 xmlns:prf="namespacename">
...snip...
</prf:XmlElement1>
Form2
<XmlElement1 xmlns="namespacename">
...snip...
</XmlElement1>
I'm interacting with a web service that chokes on an element when its namespace is described as in Form2, but works fine if the namespace is described as in Form1. Unfortunately, I haven't been able to convince my web service framework to emit the element like Form1; the best I can get so far is Form2. It seems to me that both forms are correctly specifying the namespace, and that this is probably a bug on the web service's side.
回答1:
@marc_s already explained in his comment what is the difference.
Also @JohnSaunders is probably correct in that the service is broken. One possible reason why using a default namespace breaks a document is that the service probably relies on hardcoded namespace prefixes. You can verify this by serving it a document that uses the same namespace but a different prefix than prf
(or whatever it always uses). Also note that if you do not declare a namespace for a prefix, make sure that you do not use that prefix anywhere, in element names or attribute names. Using an undeclared prefix renders your document not (namespace) well-formed.
Also properly functional XML services might depend on fixed namespace prefixes, if the documents are validated with a DTD. Unlike other schema languages, DTD is not namespace aware so handling the namespace declarations is difficult and therefore the location of the declaration and the prefix might be fixed.
来源:https://stackoverflow.com/questions/8597473/difference-between-using-an-xml-namespace-prefix-and-specifying-an-xmlns-blah