【问题标题】:Unique Particle Attribution Error with <any>, <element>, and <choice><any>、<element> 和 <choice> 的唯一粒子归因错误
【发布时间】:2013-07-10 20:50:20
【问题描述】:

目标:

元素 octave 可以有 either 子节点 query any 命名空间http://www.website.com/main 中的子节点。

我不知道这是在哪里/如何违反了独特的粒子属性...没有其他元素称为 octave 或称为查询的子节点。查询不是一个全局元素,它只用于八度。

错误:

"cos-nonambig: "http://www.website.com/main":query 和 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" />

【问题讨论】:

    标签: xml xsd xml-validation xsd-validation


    【解决方案1】:

    你描述的行为可以通过这样声明'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 中的任意元素匹配。如果 'query' 应该仅作为元素 '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>
    

    由于“查询”的声明不再生成扩展名称在“主”命名空间中的元素声明,它不再与通配符竞争。

    您也可以迁移到 XSD 1.1,它放宽了“唯一粒子归属”规则,规定元素声明和引用优先于通配符匹配,从而使您现有的声明合法。

    【讨论】:

      【解决方案2】:

      我认为问题在于:您定义了targetNamespace="http://www.website.com/main"。然后使用&lt;xs:any... 声明这个命名空间中可以有任何东西。但在这个命名空间中也是query 元素,因此它可能存在,因为使用&lt;xs:element 的直接声明以及使用&lt;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>
      

      这显然是一个错误。

      【讨论】:

      • 所以&lt;any&gt; 无法区分全局元素和子节点?这很令人沮丧。我想一种解决方法是将&lt;query&gt; 放在不同的命名空间中并导入它,但我不喜欢在没有逻辑、组织目的的情况下使用额外的命名空间。就这样。
      • 到目前为止我还不知道,但我猜解析器并没有检查 &lt;any&gt; 到底是什么。这似乎是&lt;choice&gt;&lt;any&gt; 组合的问题(&lt;sequence&gt;&lt;any&gt; 的组合有效)。
      猜你喜欢
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多