【问题标题】:What is wrong with this xsd?这个xsd有什么问题?
【发布时间】:2009-12-18 08:36:59
【问题描述】:

以下 XSD 应验证 favorite_fruit 元素的 name 属性应仅在 fruits 元素中包含 namesfruit。这是 XSD:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="Fruit">
    <xsd:attribute name="name" type="xsd:string"/>
  </xsd:complexType>  

  <xsd:complexType name="FruitArray">
    <xsd:sequence>
      <xsd:element name="fruit" minOccurs="0" maxOccurs="unbounded" type="Fruit"/>
    </xsd:sequence>
  </xsd:complexType> 

  <xsd:element name="fruit_basket">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="fruits" minOccurs="1" maxOccurs="1" type="FruitArray"/>
        <xsd:element name="favourite_fruit" minOccurs="1" maxOccurs="1">
          <xsd:complexType>
            <xsd:attribute name="name" use="required"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType> 

    <xsd:key name="fruit_lookup">
      <xsd:selector xpath="fruits/fruit"/>
      <xsd:field xpath="@name"/>
    </xsd:key>

    <xsd:keyref name="favourite_fruit_constraint" refer="fruit_lookup">
      <xsd:selector xpath="favourite_fruit"/>
      <xsd:field xpath="@name"/>
    </xsd:keyref>    
  </xsd:element>
</xsd:schema>

以下xml应该是有效的,但验证后无效:

<fruit_basket>
  <fruits>
    <fruit name="Apple"/>
    <fruit name="Peach"/>
    <fruit name="Bananna"/>
  </fruits>

  <favourite_fruit name="Apple"/>
</fruit_basket>

有什么想法吗?我的直觉是我的 xpath 有问题。 PS:我正在使用 lxml 来针对 xsd 验证 xml。

【问题讨论】:

  • 你能给我们看看 XML 吗?
  • w3.org/2001/03/webdata/xsv 的 W3C 模式验证器说没问题,所以我猜你的工具坏了和/或太挑剔了。
  • xml在上面的帖子中。
  • tools.decisionsoft.com/schemaValidate/results.jsp 的验证器也返回无效结果。
  • 我发现了问题。匿名复杂类型指定没有类型的属性“名称”。 Fruit 类型有一个属性“name”,其类型为:xsd:string。因为这两个属性没有相同的类型,所以它们无法匹配。因此,将匿名复杂类型属性定义更改为: 有效。如果有人将此添加为答案,我会将其标记为正确:)

标签: validation xpath xsd


【解决方案1】:

匿名复杂类型指定没有类型的属性“名称”。 Fruit 类型有一个属性“name”,其类型为:xsd:string。因为这两个属性没有相同的类型,所以它们无法匹配。因此将匿名复杂类型属性定义更改为 : 有效。

【讨论】:

    【解决方案2】:

    您还没有为favourite_fruitxsd:element 指定类型。因此架构无法针对Fruit 类型进行验证:

        <xsd:element name="favourite_fruit" minOccurs="1" maxOccurs="1">
          <xsd:complexType>
            <xsd:attribute name="name" use="required"/>
          </xsd:complexType>
        </xsd:element>
    

    您没有将类型限制为Fruit。这应该会更好:

       <xsd:element name="favourite_fruit" minOccurs="1" maxOccurs="1" Type="Fruit" />
    

    【讨论】:

    • 这里不需要命名类型,它是匿名的、嵌套的复杂类型。
    • 他想根据他的类型来验证。
    • 不,实际上我只想验证我的匿名类型的名称属性是否等于水果数组中水果类型的名称之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2012-05-26
    • 2021-08-20
    • 2015-09-23
    • 2021-02-27
    • 2012-03-10
    • 2010-11-01
    相关资源
    最近更新 更多