为什么我收到有关子元素命名空间的错误?

Why am I getting an error about namespace of child element?

当我尝试验证此代码时,出现错误...

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel">
    <h:existingRooms>
        <room>101</room>
        <room>102</room>
        <room>201</room>
    </h:existingRooms>
</h:hotel>

错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element '{"hotel":existingRooms}'. One of '{existingRooms}' is expected.

当我将 XML 编辑成这个时,我不再收到错误消息:

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel">
    <existingRooms>
        <room>101</room>
        <room>102</room>
        <room>201</room>
    </existingRooms>
</h:hotel>

我的 XSD 是(有错误)是:

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel"
    targetNamespace="hotel">
    <element name="hotel">
        <complexType>
            <sequence>                
                <element name="existingRooms">
                    <complexType>
                        <sequence>
                            <element name="room" type="integer" minOccurs="0" maxOccurs="unbounded"/>
                        </sequence>
                    </complexType>
                </element>      
            </sequence>
        </complexType>
    </element>
</schema>

如果将 elementFormDefault="qualified" 添加到架构的根目录,

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        xmlns:h="hotel"
        targetNamespace="hotel">
  <element name="hotel">
    <complexType>
      <sequence>                
        <element name="existingRooms">
          <complexType>
            <sequence>
              <element name="room" type="integer" minOccurs="0" maxOccurs="unbounded"/>
            </sequence>
          </complexType>
        </element>      
      </sequence>
    </complexType>
  </element>
</schema>

然后您将消除意外错误,但您接下来会发现以下错误:

[Error] try.xml:6:15: cvc-complex-type.2.4.a: Invalid content was found starting with element 'room'. One of '{"hotel":room}' is expected.

但您可能已经预料到这一点并且可以轻松纠正它,

<?xml version="1.0"?>
<h:hotel xmlns:h="hotel"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="hotel try.xsd">
    <h:existingRooms>
        <h:room>101</h:room>
        <h:room>102</h:room>
        <h:room>201</h:room>
    </h:existingRooms>
</h:hotel>

并让您的 XML 根据要求成功验证您的 XSD。

另见:

  • What does elementFormDefault do for XML/When is it used?

这是因为在您的架构中 elementFormDefault 隐式设置为 unqualified

表示你设置的schema等价于下面的声明:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel" targetNamespace="hotel" 
    elementFormDefault="unqualified" attributeFormDefault="unqualified">

由此可以理解,只有全局元素是合格的,所有其他元素都是不合格的。

如果您需要限定所有元素(在具有 uri hotel 的命名空间中),您可以通过将 elementFormDefault 设置为 qualified 来修改您的模式:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
    xmlns:h="hotel" targetNamespace="hotel" 
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

有效的 XML 实例将是:

<?xml version="1.0"?>
<h:hotel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="hotel hotel.xsd" xmlns:h="hotel">
 <h:existingRooms>
  <h:room>101</h:room>
  <h:room>102</h:room>
  <h:room>201</h:room>
 </h:existingRooms>
</h:hotel>

就像这个替代实例:

<?xml version="1.0"?>
<hotel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="hotel hotel.xsd" xmlns="hotel">
 <existingRooms>
  <room>101</room>
  <room>102</room>
  <room>201</room>
 </existingRooms>
</hotel> 

如果您只需要 existingRooms 元素进行限定,则将其设置为您的模式:

<element name="existingRooms" form="qualified">

并且您提供的第一个 XML 将有效。

请注意:

  • 通常,命名空间 URI 的格式应为 http://some.url/hotel