我的客户正在与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吗?
答案 0 :(得分:2)
但是根元素本身正在使用“stream”命名空间 看起来很奇怪这是有效的XML吗?
根元素本身使用stream
命名空间并不奇怪,但是......
有效必须与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作为验证的基础。