2012-05-25 95 views
2

我遇到一些问题,根据我定义的模式验证xml。奇怪的是,只有当我使用默认的命名空间xmlns="http://retis.sssup.it/duck-lab"时,验证才会失败,如果我将其定义为xmlns:dl="http://retis.sssup.it/duck-lab",它就像是魅力一样。XML验证失败的属性

当我使用空命名空间验证不但不能与以下消息的属性:

cvc-complex-type.3.2.2: Attribute 'data_len' is not allowed to appear in element 'data'.  
cvc-complex-type.4: Attribute 'data_len' must appear on element 'data'. 

有效的XML:

<dl:duck xmlns:dl="http://retis.sssup.it/duck-lab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://retis.sssup.it/duck-lab ../duck.xsd "> 
... 
     <dl:data dl:data_len="1" dl:data_name="name uint" dl:data_type="uint16" dl:endianess="big-endian"/> 

无效的XML:

<duck xmlns="http://retis.sssup.it/duck-lab" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://retis.sssup.it/duck-lab ../duck.xsd "> 
... 
    <data data_len="1" data_name="name uint" data_type="uint16" endianess="big-endian"/> 

--EDIT--

DUCK.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab" 
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified"> 

    <include schemaLocation="datatypes.xsd"/> 
    <include schemaLocation="duck_message.xsd"/> 

    <complexType name="DuckDefinitionType" block="#all" final="#all"> 
     <sequence> 
      <element type="dl:MessageType" name="message_description" form="qualified"/> 
     </sequence> 
    </complexType> 

    <element name="duck" type="dl:DuckDefinitionType" /> 

</schema> 

DATATYPES.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab" 
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified"> 
     <attribute name="data_name" type="string"/> 
    <attribute name="data_len" type="nonNegativeInteger"/> 
    <attribute name="data_type" type="string"/> 
    <attribute name="endianess" type="string"/> 
     <element name="data"> 
     <complexType> 
      <attribute ref="dl:data_name" use="required"/> 
      <attribute ref="dl:data_len" use="required"/> 
      <attribute ref="dl:data_type" use="required"/> 
      <attribute ref="dl:endianess" use="required"/> 
     </complexType> 
    </element> 
</schema> 

DUCK_MESSAGE.XSD

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://retis.sssup.it/duck-lab" 
    xmlns:dl="http://retis.sssup.it/duck-lab" elementFormDefault="qualified"> 

    <include schemaLocation="datatypes.xsd"></include> 
    <complexType name="MessageType"> 
     <sequence maxOccurs="unbounded"> 
      <element ref="dl:data"></element> 
     </sequence> 
    </complexType> 
</schema> 

显然,我可以绕过这个问题定义非空的命名空间,但我想了解什么是错的。

非常感谢。

+0

向我们展示模式的其余部分(特别是'schema'元素本身,因为这可能看起来像是具有限定属性的问题。 –

+0

用模式的其余部分更新了问题。 – hara

回答

2

缺省名称空间的处理对于属性是不同的 - 没有名称空间前缀的属性不与默认名称空间相关联,它没有名称空间,请参阅here

这意味着,在无效XML的各种属性data-lendata-name等没有命名空间,而架构宣称他们是在http://retis.sssup.it/duck-lab命名空间由于form="qualified"指令。