【发布时间】:2017-10-20 01:17:10
【问题描述】:
我想验证两个字段在同一个 complexType 中始终具有唯一值。我想在我的 XSD 中验证这一点,而不是直接在代码中进行。
我知道我不能在同一个 complexType 中使用类型“xs:ID”两次,不幸的是我不能使标签工作。
我已经有一个属性“bookId”,在 complexType 中使用类型“xs:ID”,所以我想使用标签来保证另一个元素“bookName”的唯一值。
我有以下结构:
<xs:complexType name="book">
<xs:sequence>
<xs:element name = "bookName" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string"
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name = "bookPrice" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:decimal"
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="bookId" type="xs:ID" use="required"/>
</xs:complexType>
我已经尝试过执行以下操作,但没有成功:
<xs:complexType name="book">
<xs:sequence>
<xs:element name = "bookName" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
</xs:simpleType>
<xs:unique name="uniqueBookName">
<xs:selector xpath="book"/>
<xs:field xpath="bookName"/>
</xs:unique>
</xs:element>
<xs:element name = "bookPrice" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:decimal"
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="bookId" type="xs:ID" use="required"/>
</xs:complexType>
我正在使用 xml 版本 1.0。我不知道我可以从这里再尝试什么才能让它发挥作用。
有人对如何实现这一点有提示吗?非常感谢。
【问题讨论】:
标签: xml validation jakarta-ee xsd