【问题标题】:Unique element in XSD schemaXSD 架构中的唯一元素
【发布时间】:2017-06-06 17:25:43
【问题描述】:

我正在编写一个 xml 架构。我希望某个节点基于某些内部元素是唯一的。我写了下面的架构,但它不工作。我可以多次添加同一本书/员工。如果有人输入重复元素(节点),我希望我的书名/员工应该是唯一的并且通过异常 有人可以帮我吗?提前致谢。

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

<xs:complexType name="employee">
    <xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="employees">
    <xs:sequence>
        <xs:element name="employees" type="employee" minOccurs="0" maxOccurs="unbounded">
            <xs:unique name="uniqueId">
                <xs:selector xpath="employee"/>
                <xs:field xpath="@id"/>
            </xs:unique>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="book">
    <xs:sequence>
        <xs:attribute name="author" type="xs:string" use="required"/>
        <xs:attribute name="title" type="xs:string" use="required"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="bookList">
    <xs:sequence>
        <xs:element name="bookList" type="book" minOccurs="0" maxOccurs="unbounded">
            <xs:unique name="uniqueBook">
                <xs:selector xpath="book"/>
                <xs:field xpath="@title"/>
            </xs:unique>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:element name="Library">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="bookList" type="bookList"/>
            <xs:element name="employees" type="employees"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

【问题讨论】:

  • 你能发布一个示例 xml 吗?
  • 为什么你没有接受你提出的 20 个问题的答案?
  • @jayvee w3schools.com">

标签: xml xsd


【解决方案1】:

您的架构表明employee 元素的id 属性必须是唯一的,但是在您的架构和实例中(作为评论发布),employee 元素没有 id 属性;相反,它有一个title 属性。

此外,您的 employee 和 book 元素在架构中声明为位于命名空间中(尽管您的实例中缺少此命名空间),并且您在定义选择器/字段 XPath 表达式时忽略了此命名空间您的唯一性约束。

【讨论】:

  • 抱歉,我不会尝试调试已知错别字的代码。请让我们正确回答您的问题。
  • 我知道问题与选择器/字段 xpath 有关。你能帮我吗?我是这个东西的新手。
  • 我关注了stackoverflow.com/questions/7717515/…,因为我的案例书根据书名应该是独一无二的。这就是为什么选择器 xpath 是书,字段 xpath 是标题
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多