【问题标题】:Why is the 'null-namespace' expected for an xml element that was defined to 'have' a namespace in the schema?为什么定义为在架构中“拥有”命名空间的 xml 元素需要“空命名空间”?
【发布时间】:2013-10-16 01:11:14
【问题描述】:

以下 xml 文档和架构是从首先出现问题的更大的架构中总结出来的:

xml文档:

<f2b:Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:f2b="http://www.outerfoospace.com/schemas/foo2bar" xsi:schemaLocation="http://www.outerfoospace.com/schemas/foo2bar f2b-0-0-0.xsd">
    <f2b:Bar>text</f2b:Bar>
</f2b:Foo>

架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f2b="http://www.outerfoospace.com/schemas/foo2bar" targetNamespace="http://www.outerfoospace.com/schemas/foo2bar">
    <xs:complexType name="T_Foo">
        <xs:sequence>
            <xs:element name="Bar" xmlns="f2b"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="Foo" type="f2b:T_Foo"/>
</xs:schema>

当我根据架构验证文档时,我在 &lt;f2b:Bar&gt;-元素上收到一条错误消息,指出不应使用 &lt;f2b:Bar&gt;。为了使文档有效,我必须将&lt;Bar&gt;-元素显式(!)放入'null-namespace':&lt;Bar xmlns=""&gt;。 我不明白为什么应该期望一个空命名空间元素。该模式为所有元素定义了一个命名空间,对于&lt;Foo&gt;-元素,验证器确实需要f2b-命名空间。但即使我像上面那样在&lt;Bar&gt;-element 定义中明确设置命名空间,验证器仍然期望xmlns="" 用于Bar-element。 我尝试了架构和文档的几种变体,但没有找到答案 - 我根本无法猜测为什么在这种特殊情况下需要空命名空间。

任何提示表示赞赏...... ekke

【问题讨论】:

    标签: xml xsd xml-namespaces


    【解决方案1】:

    简答:将 elementFormDefault="qualified" 添加到架构的根 xs:schema 标记中。

    更长的答案:默认情况下,只有顶级“全局”元素声明采用架构的目标命名空间。复杂类型内的“本地”元素声明不在命名空间中。您可以使用xs:element 标记上的form 属性覆盖一个特定的本地元素,或者使用xs:schema 上的elementFormDefault 全局覆盖所有元素。

    【讨论】:

      猜你喜欢
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 2011-04-17
      相关资源
      最近更新 更多