【问题标题】:Namespace definition in root element which is also namespaced - valid XML?根元素中的命名空间定义也是命名空间 - 有效的 XML?
【发布时间】:2013-10-28 16:43:30
【问题描述】:

我的客户正在与Facebook chat server 进行友好的 XMPP 对话,并收到如下所示的 XML 片段:

<stream:stream xmlns:stream='http://etherx.jabber.org/streams' from='chat.facebook.com' id='1' version='1.0' >
</stream:stream>

所以在根元素上有一个命名空间定义“流”。到目前为止一切顺利。

但是根元素本身使用的是“stream”命名空间,这看起来很奇怪。这是有效的 XML 吗?

我正在使用的 XML 库 (dart-xml) 抱怨它,我想知道是否正确,或者该库是否有 bug

【问题讨论】:

  • 命名空间的范围是在其中声明和包含元素的元素。因此,在声明它的元素中使用 ns 是完全有效的。

标签: xml xmpp dart xml-namespaces


【解决方案1】:

但是根元素本身使用的是“stream”命名空间,它 看起来很奇怪。这是有效的 XML 吗?

根元素本身使用stream 命名空间并不奇怪,但是...

Valid 必须与 XSD 相关,并且 XSD 必须与 XML 实例相关联。我看到命名空间指定的端点上有一个 XSD:http://etherx.jabber.org/streams.xsd

建立关联的常用方法是使用xsi:schemaLocation 属性:

<stream:stream xmlns:stream='http://etherx.jabber.org/streams'
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://etherx.jabber.org/streams http://etherx.jabber.org/streams.xsd"
               from='chat.facebook.com' id='1' version='1.0'>
</stream:stream> 

然后验证可以找到要使用的 XML Schema,但是,有一个问题:

[Error] streams.xsd:23:21: cos-nonambig: WC["urn:ietf:params:xml:ns:xmpp-tls"] and WC[##other:"http://etherx.jabber.org/streams"] (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.
[Error] streams.xsd:74:21: cos-nonambig: "urn:ietf:params:xml:ns:xmpp-streams":text and WC[##other:"http://etherx.jabber.org/streams"] (or elements from their substitution group) violate "Unique Particle Attribution". During validation against this schema, ambiguity would be created for those two particles.

Unique Particle Attribution 是 XSD 的必需约束。因此,为了回答您的问题,我们不能说 XML 是有效的,因为我们没有作为验证依据的有效 XSD。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多