使用 XML 名称空间前缀和指定 xmlns="blah" 之间的区别作为属性 [英] Difference between using an XML namespace prefix and specifying an xmlns="blah" as an attribute

查看:33
本文介绍了使用 XML 名称空间前缀和指定 xmlns="blah" 之间的区别作为属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于命名空间,以下 xml 片段之间有什么(如果有)区别?

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>

我正在与一个 Web 服务进行交互,该服务在元素的命名空间描述为 Form2 时阻塞元素,但如果命名空间描述为 Form1,则工作正常.不幸的是,我无法说服我的 Web 服务框架发出类似 Form1 的元素;到目前为止我能得到的最好的是 Form2.在我看来,这两种形式都正确指定了命名空间,这可能是 Web 服务方面的错误.

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.

推荐答案

@marc_s 已经在他的评论中解释了有什么区别.

@marc_s already explained in his comment what is the difference.

@JohnSaunders 也可能是正确的,因为服务已损坏.使用默认命名空间破坏文档的一个可能原因是该服务可能依赖于硬编码的命名空间前缀.您可以通过为它提供一个文档来验证这一点,该文档使用与 prf(或它始终使用的任何内容)相同的名称空间但不同的前缀.另请注意,如果您没有为前缀声明命名空间,请确保不要在元素名称或属性名称的任何地方使用该前缀.使用未声明的前缀会使您的文档(命名空间)格式不正确.

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.

如果文档使用 DTD 进行验证,则正常运行的 XML 服务也可能依赖于固定的名称空间前缀.与其他模式语言不同,DTD 不知道命名空间,因此处理命名空间声明很困难,因此声明的位置和前缀可能是固定的.

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.

这篇关于使用 XML 名称空间前缀和指定 xmlns="blah" 之间的区别作为属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆