2015-11-16 40 views
0

當我試圖驗證此代碼,我得到一個錯誤......爲什麼我會收到有關子元素命名空間的錯誤?

<?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> 
+0

我看不到你的原始XML和編輯一個... – potame

+0

任何區別我不好,我貼了2次相同的代碼,現在我修好了。 – stijnpiron

回答

0

,因爲在模式elementFormDefault隱含設置爲unqualified

這意味着你已經設置了模式等同於以下聲明:

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

因此,應理解,只有全局元素是合格的,所有其他元素保持不合格。

如果您需要對合格的所有元素(要與URI hotel命名空間,你可以通過設置elementFormDefaultqualified修改架構:

<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
+0

是的,最後一個選項是我的問題的解決方案:-) 您建議的URI表單的優勢在我使用的表單上方有什麼優勢? – stijnpiron

+0

沒有具體優勢。命名空間的值應該是這裏指定的URI http://www.w3.org/TR/REC-xml-names/ – potame

1

如果添加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成功請求。

參見:

相關問題