【问题标题】:Schema xs:unique is not being validatedSchema xs:unique 未被验证
【发布时间】:2013-03-24 09:06:04
【问题描述】:

我有一个简单的 xml 文件

<form xmlns="http://www.example.org/form-reader/form-description"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/form-reader/form-description form-description.xsd">
    <pages amount="1"/>
    <pages amount="1"/>
</form>

我为它写了架构

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/form-reader/form-description"
xmlns="http://www.example.org/form-reader/form-description"
elementFormDefault="qualified">
    <xs:complexType name="pagesType">
    <xs:attribute name="amount" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="formType">
    <xs:sequence minOccurs="1">
        <xs:element name="pages" minOccurs="0" maxOccurs="unbounded"
        type="pagesType" />
    </xs:sequence>
    </xs:complexType>

    <xs:element name="form" type="formType">
    <xs:unique name="pageNumberUnique">
        <xs:selector xpath="pages" />
        <xs:field xpath="@amount" />
    </xs:unique>
    </xs:element>
</xs:schema>

我想验证 amount 属性的唯一性,但在所有验证器中,我的 xml 文件似乎有效,但它不应该,为什么?

解决方案

应用@PetruGardea 修复后我的架构如下所示:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/form-reader/form-description"
targetNamespace="http://www.example.org/form-reader/form-description"
xmlns="http://www.example.org/form-reader/form-description"
elementFormDefault="qualified">
    <xs:complexType name="pagesType">
    <xs:attribute name="amount" type="xs:string" use="required" />
    </xs:complexType>

    <xs:complexType name="formType">
    <xs:sequence minOccurs="1">
    <xs:element name="pages" minOccurs="0" maxOccurs="unbounded"
    type="pagesType" />
    </xs:sequence>
    </xs:complexType>

    <xs:element name="form" type="formType">
    <xs:unique name="pageNumberUnique">
    <xs:selector xpath="tns:pages" />
    <xs:field xpath="@amount" />
    </xs:unique>
    </xs:element>
</xs:schema>

【问题讨论】:

    标签: xml xsd schema unique


    【解决方案1】:

    你必须为你的 targetNamespace 定义一个别名,并在你的 xpath 中为选择器和字段使用它。请参阅此answer on SO,并阅读帖子和我制作的 cmets 了解更多详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 2012-12-02
      • 2018-06-09
      • 2013-03-22
      • 2015-10-24
      • 2019-10-18
      • 2015-06-18
      • 2012-01-08
      相关资源
      最近更新 更多