简体   繁体   English

使元素内容在 xsd 中唯一

[英]Make content of element unique in xsd

I have an element:我有一个元素:

<xsd:element name="tags" type="tagsType"></xsd:element>

This is tagsType :这是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>

How I have restricted tagname attribute to be unique, but I want to make content of that tag also unique.我如何将tagname名属性限制为唯一,但我想让该标记的内容也唯一。 Example:例子:

<tags>
    <t>tag1</t>
    <t>tag1</t>
    <t>tag2</t>
</tags>

This should not validate, because of duplicate tag1 .这不应该验证,因为重复的tag1 This should validate:这应该验证:

<tags>
    <t>tag1</t>
    <t>tag2</t>
    <t>tag3</t>
</tags>

How do I achieve this?我如何实现这一目标?

You can achieve the desired result with the following XSD.您可以使用以下 XSD 获得所需的结果。
It uses the xsd:unique element to make sure that the values are unique.它使用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>

This XSD validates the second XML and fails on the first one.此 XSD 验证第二个 XML 并在第一个上失败。


The xsd:unique element has two sub-elements: xsd:unique元素有两个子元素:

The xsd:unique element MUST contain the following (in order): xsd:unique 元素必须包含以下内容(按顺序):

  • 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)一个也是唯一一个 xsd:selector 元素(包含一个 XPath 表达式,该表达式指定一组元素,字段指定的值必须在其中唯一)
  • 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)一个或多个 xsd:field 元素(包含一个 XPath 表达式,该表达式指定对于选择器元素指定的元素集必须唯一的值)

If you want every X in a Y to have a unique value for Z, then:如果您希望 Y 中的每个 X 对 Z 具有唯一值,则:

(a) the declaration of Y should hold the xs:unique constraint (a) Y 的声明应该持有xs:unique约束

(b) the selector should be a path expression that selects X starting at Y (b) 选择器应该是从 Y 开始选择 X 的路径表达式

(c) the field should be a path expression that selects Z starting at X. (c) 该字段应该是从 X 开始选择 Z 的路径表达式。

So the fundamental mistake you've made is to define the constraint at the wrong level.因此,您所犯的根本错误是将约束定义在错误的级别。 xs:unique doesn't belong on the thing you want to be unique, it belongs on the containing element within which it needs to be unique. xs:unique不属于你想要唯一的东西,它属于它需要唯一的包含元素。

That's because the validity of an element depends only on its content, and not on its context.这是因为元素的有效性仅取决于其内容,而不取决于其上下文。 If two X's have the same value for Z, it's the containing Y that's invalid.如果两个 X 的 Z 值相同,则包含的 Y 无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM