我的客户正在与 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”。到目前为止一切顺利。
但是根元素本身正在使用“stream”命名空间,这看起来很奇怪。这是有效的 XML 吗?
最佳答案
But the root element itself is using the "stream" namespace, which seems odd. Is this valid 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 架构,但是有一个问题:
[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 作为验证的基础。
关于xml - 根元素中的命名空间定义也是命名空间 - 有效的 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473373/