<any>,<element>和<choice> </choice> </element> </any>的唯一粒子属性错误

时间:2013-07-10 20:50:20

标签: xml xsd xml-validation xsd-validation

目标:

元素 octave 可以 子节点查询 任何命名空间http://www.website.com/main中的子节点。

我不知道这在何处/如何违反独特的粒子归属...没有其他元素称为八度或子节点称为查询。查询是一个全局元素,它仅用于八度音程。

错误:

“cos-nonambig:”http://www.website.com/main“:查询和WC [”http://www.website.com/main“](或来自其替换组的元素)违反”唯一粒子归因“。在针对此架构进行验证时,歧义将为这两个粒子创建。“

架构:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema
    xmlns="http://www.website.com/main"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.website.com/main" 
    attributeFormDefault="unqualified" elementFormDefault="qualified"
    >

<xs:complexType name="octave" >
    <xs:choice>
        <xs:element name="query" type="xs:string" />
        <xs:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.website.com/main" processContents="strict" />
    </xs:choice>
    <xs:attribute name="id" type="xs:string" use="optional" />
</xs:complexType>

<xs:element name="octave" type="octave" />

2 个答案:

答案 0 :(得分:2)

您描述的行为可以通过以这种方式声明'octave'来获得:

<xs:complexType name="octave" >
  <xs:choice>
    <xs:any minOccurs="0" 
            maxOccurs="unbounded" 
            namespace="http://www.website.com/main" 
            processContents="strict" />
  </xs:choice>
  <xs:attribute name="id" 
                type="xs:string" 
                use="optional" />
</xs:complexType>

并将查询声明为全局元素:

<xs:element name="query" type="xs:string" />

由于您定义的查询元素位于命名空间http://www.website.com/main中,因此它被通配符接受,并且从“八度”类型的角度来看,没有迫切需要包含元素引用它在选择中。

此设计的缺点是使所有现有通配符都可以访问“查询”,这些通配符匹配命名空间http://www.website.com/main中的任意元素。如果重要的是“查询”应该仅作为元素'octave'的子元素和相同类型的其他元素出现,那么您可以考虑使本地'query'元素不合格:

<xs:complexType name="octave" >
  <xs:choice>
    <xs:element name="query" 
                type="xs:string" 
                form="unqualified"/>
    <xs:any minOccurs="0" 
            maxOccurs="unbounded" 
            namespace="http://www.website.com/main" 
            processContents="strict" />
  </xs:choice>
  <xs:attribute name="id" 
                type="xs:string" 
                use="optional" />
</xs:complexType>

由于'query'的声明不再生成一个元素声明,其扩展名称在'main'命名空间中,因此它不再与通配符竞争。

你也可以转向XSD 1.1,它放宽了“独特粒子归因”规则,它说元素声明和引用优先于通配符匹配,从而使你现有的声明合法。

答案 1 :(得分:1)

我认为问题在于:您定义targetNamespace="http://www.website.com/main"。然后使用<xs:any...声明此命名空间中可以有任何内容。但是在这个命名空间中也是query元素,所以它可以存在于那里,因为使用<xs:element直接声明,也因为使用<xs:any的“间接”声明。

对于解析器,它可能与您声明的内容相同:

<xs:complexType name="a">
    <xs:choice>
        <xs:element name="b" type="xs:string" />
        <xs:element name="b" type="xs:string" />
    </xs:choice>
</xs:complexType>

这显然是一个错误。