使元素的内容在 xsd 中唯一
Make content of element unique in xsd
我有一个元素:
<xsd:element name="tags" type="tagsType"></xsd:element>
这是tagsType
:
<xsd:complexType name="tagsType">
<xsd:sequence>
<xsd:element name="t" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="tagname" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:key name="tagKey">
<xsd:selector xpath="tags/tag"/>
<xsd:field xpath="@tagname"/>
</xsd:key>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
我如何限制 tagname
属性是唯一的,但我想使该标签的内容也是唯一的。示例:
<tags>
<t>tag1</t>
<t>tag1</t>
<t>tag2</t>
</tags>
这不应该验证,因为重复 tag1
。这应该验证:
<tags>
<t>tag1</t>
<t>tag2</t>
<t>tag3</t>
</tags>
如何实现?
您可以通过以下 XSD.
获得想要的结果
它使用 xsd:unique
元素来确保值是唯一的。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="tagsType">
<xsd:sequence>
<xsd:element name="t" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="tags" type="tagsType">
<xsd:unique name="t_unique" >
<xsd:selector xpath="t"/>
<xsd:field xpath="."/>
</xsd:unique>
</xsd:element>
</xsd:schema>
这个 XSD 验证了第二个 XML 而在第一个上失败了。
xsd:unique 元素有两个子元素:
The xsd:unique element MUST contain the following (in order):
- one and only one xsd:selector element (contains an XPath expression that specifies the set of elements across which the values specified by field must be unique)
- one or more xsd:field elements (contains an XPath expression that specifies the values that must be unique for the set of elements specified by the selector element)
如果您希望 Y 中的每个 X 都具有唯一的 Z 值,则:
(a) Y 的声明应该包含 xs:unique
约束
(b) 选择器应该是选择从 Y 开始的 X 的路径表达式
(c) 该字段应该是选择从 X 开始的 Z 的路径表达式。
所以你犯的根本错误是在错误的级别定义约束。 xs:unique
不属于您想要唯一的事物,它属于需要唯一的包含元素。
这是因为元素的有效性仅取决于其内容,而不取决于其上下文。如果两个 X 的 Z 值相同,则包含 Y 的值无效。
我有一个元素:
<xsd:element name="tags" type="tagsType"></xsd:element>
这是tagsType
:
<xsd:complexType name="tagsType">
<xsd:sequence>
<xsd:element name="t" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="tagname" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:key name="tagKey">
<xsd:selector xpath="tags/tag"/>
<xsd:field xpath="@tagname"/>
</xsd:key>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
我如何限制 tagname
属性是唯一的,但我想使该标签的内容也是唯一的。示例:
<tags>
<t>tag1</t>
<t>tag1</t>
<t>tag2</t>
</tags>
这不应该验证,因为重复 tag1
。这应该验证:
<tags>
<t>tag1</t>
<t>tag2</t>
<t>tag3</t>
</tags>
如何实现?
您可以通过以下 XSD.
获得想要的结果
它使用 xsd:unique
元素来确保值是唯一的。
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="tagsType">
<xsd:sequence>
<xsd:element name="t" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="tags" type="tagsType">
<xsd:unique name="t_unique" >
<xsd:selector xpath="t"/>
<xsd:field xpath="."/>
</xsd:unique>
</xsd:element>
</xsd:schema>
这个 XSD 验证了第二个 XML 而在第一个上失败了。
xsd:unique 元素有两个子元素:
The xsd:unique element MUST contain the following (in order):
- one and only one xsd:selector element (contains an XPath expression that specifies the set of elements across which the values specified by field must be unique)
- one or more xsd:field elements (contains an XPath expression that specifies the values that must be unique for the set of elements specified by the selector element)
如果您希望 Y 中的每个 X 都具有唯一的 Z 值,则:
(a) Y 的声明应该包含 xs:unique
约束
(b) 选择器应该是选择从 Y 开始的 X 的路径表达式
(c) 该字段应该是选择从 X 开始的 Z 的路径表达式。
所以你犯的根本错误是在错误的级别定义约束。 xs:unique
不属于您想要唯一的事物,它属于需要唯一的包含元素。
这是因为元素的有效性仅取决于其内容,而不取决于其上下文。如果两个 X 的 Z 值相同,则包含 Y 的值无效。