Difference between using an XML namespace prefix and specifying an xmlns=“blah” as an attribute

核能气质少年 提交于 2019-12-22 11:22:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!