确保XML模式(XSD)的元素和属性的唯一性

时间:2014-05-21 10:32:13

标签: xml xsd

我试图确保我的号码"元素内容和" playId"属性在同一级别上是唯一的。我已阅读其他帖子,但我无法找到完成此操作的技巧。

这是我的示例XML与相应的架构:

<tic:tickets xmlns:tic="http://www.ti.bfh.ch/i1p/akz/schema/ticket2">
    <tic:ticket ticketId="0">
        <tic:plays>
            <tic:play playId="0">
                <tic:numbers>
                    <tic:number>1</tic:number>
                    <tic:number>2</tic:number>
                    <tic:number>3</tic:number>
                    <tic:number>4</tic:number>
                    <tic:number>5</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>1</tic:star>
                    <tic:star>2</tic:star>
                </tic:stars>
            </tic:play>
            <tic:play playId="0">
                <tic:numbers>
                    <tic:number>50</tic:number>
                    <tic:number>50</tic:number>
                    <tic:number>50</tic:number>
                    <tic:number>50</tic:number>
                    <tic:number>50</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>11</tic:star>
                    <tic:star>11</tic:star>
                </tic:stars>
            </tic:play>
        </tic:plays>
        <tic:superStars>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="true">12AB</tic:superStar>
        </tic:superStars>
        <tic:validity>2</tic:validity>
        <tic:dateTime>2007-10-26T08:36:28</tic:dateTime>
    </tic:ticket>
    <tic:ticket ticketId="0">
        <tic:plays>
            <tic:play playId="100">
                <tic:numbers>
                    <tic:number>1</tic:number>
                    <tic:number>1</tic:number>
                    <tic:number>3</tic:number>
                    <tic:number>4</tic:number>
                    <tic:number>5</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>11</tic:star>
                    <tic:star>11</tic:star>
                </tic:stars>
            </tic:play>
        </tic:plays>
        <tic:superStars>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="false">12AB</tic:superStar>
            <tic:superStar selected="false">12AB</tic:superStar>
        </tic:superStars>
        <tic:validity>6</tic:validity>
        <tic:dateTime>2007-10-26T08:36:28</tic:dateTime>
    </tic:ticket>
</tic:tickets>

架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tic="http://www.ti.bfh.ch/i1p/akz/schema/ticket2"
           targetNamespace="http://www.ti.bfh.ch/i1p/akz/schema/ticket2" elementFormDefault="qualified">

    <xs:element name="tickets">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
                <xs:element name="ticket">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="tic:ticketType">
                                <xs:attribute name="ticketId" type="xs:integer" use="required"/>
                            </xs:extension>
                        </xs:complexContent>
                    </xs:complexType>
                    <xs:unique name="uniqueTicketId">
                        <xs:selector xpath="ticket"/>
                        <xs:field xpath="@ticketId"/>
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <xs:complexType name="ticketType">
        <xs:sequence>
            <xs:element name="plays">
                <xs:complexType>
                    <xs:sequence maxOccurs="7" minOccurs="1">
                        <xs:element name="play">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="numbers">
                                        <xs:complexType>
                                            <xs:sequence maxOccurs="5" minOccurs="5">
                                                <xs:element name="number" type="tic:numberType"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                        <xs:unique name="uniqueNumber">
                                            <xs:selector xpath="number"/>
                                            <xs:field xpath="."/>
                                        </xs:unique>
                                    </xs:element>
                                    <xs:element name="stars">
                                        <xs:complexType>
                                            <xs:sequence maxOccurs="2" minOccurs="2">
                                                <xs:element name="star" type="tic:starType"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                        <xs:unique name="uniqueStar">
                                            <xs:selector xpath="star"/>
                                            <xs:field xpath="."/>
                                        </xs:unique>
                                    </xs:element>
                                </xs:sequence>
                                <xs:attribute name="playId" type="xs:integer" use="required"/>
                            </xs:complexType>
                            <xs:unique name="uniquePlayId">
                                <xs:selector xpath="play"/>
                                <xs:field xpath="@playId"/>
                            </xs:unique>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="superStars">
                <xs:complexType>
                    <xs:sequence maxOccurs="4" minOccurs="4">
                        <xs:element name="superStar">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="tic:superStarType">
                                        <xs:attribute default="false" name="selected"
                                                      type="xs:boolean" use="optional"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
                <xs:unique name="uniqueSuperStar">
                    <xs:selector xpath="superStar"/>
                    <xs:field xpath="."/>
                </xs:unique>
            </xs:element>
            <xs:element name="validity" type="tic:validityType"/>
            <xs:element name="dateTime" type="xs:dateTime"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="validityType">
        <xs:restriction base="xs:integer">
            <xs:enumeration value="1"/>
            <xs:enumeration value="2"/>
            <xs:enumeration value="4"/>
            <xs:enumeration value="6"/>
            <xs:enumeration value="8"/>
            <xs:enumeration value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="numberType">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="50"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="starType">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="11"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="superStarType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>

我感谢任何建议!

2 个答案:

答案 0 :(得分:0)

您在错误的上下文中声明了唯一性约束。如果您希望每个@playId元素具有唯一的play属性,则必须在父级(plays)上下文中声明。在play本身的上下文中声明它是没有意义的,因为你无论如何都不能有重复的属性。所以你应该在下面进行更改:

<xs:element name="plays">
    <xs:complexType>
        <xs:sequence maxOccurs="7" minOccurs="1">
            <xs:element name="play">
                <xs:complexType>
                    <xs:sequence>...</xs:sequence>
                    <xs:attribute name="playId" type="xs:integer" use="required"/>
                </xs:complexType>
                <!-- move from here... -->
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="uniquePlayId"> <!-- ... to here -->
        <xs:selector xpath="tic:play"/>
        <xs:field xpath="@playId"/>
    </xs:unique>
</xs:element>

并对ticketId执行相同操作,因为它也处于错误的上下文中。

还有一个问题:您的XPath选择器不是限定。 XPath中没有默认命名空间。如果选择器不合格,它将被视为属于no-namespace,并且不会匹配任何内容。您需要对所有xpath元素选择器进行限定:tic:playtic:superStartic:number等。

另请参阅:Making Element Values Unique

答案 1 :(得分:0)

@helderdarocha 非常感谢你! 我已经玩了我插入<xs:unique>语句的级别。

这不是我的主要问题,但我不明白的是声明中不合格选择器的问题!现在我明白了背景。完美的^^!

以下是schema / xml的更正和工作版本:

XML:

<tic:tickets xmlns:tic="http://www.ti.bfh.ch/i1p/akz/schema/tickets">
    <tic:ticket ticketId="0">
        <tic:plays>
            <tic:play playId="0">
                <tic:numbers>
                    <tic:number>1</tic:number>
                    <tic:number>2</tic:number>
                    <tic:number>3</tic:number>
                    <tic:number>49</tic:number>
                    <tic:number>50</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>1</tic:star>
                    <tic:star>11</tic:star>
                </tic:stars>
            </tic:play>
            <tic:play playId="1">
                <tic:numbers>
                    <tic:number>1</tic:number>
                    <tic:number>2</tic:number>
                    <tic:number>3</tic:number>
                    <tic:number>49</tic:number>
                    <tic:number>50</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>1</tic:star>
                    <tic:star>11</tic:star>
                </tic:stars>
            </tic:play>
        </tic:plays>
        <tic:superStars>
            <tic:superStar selected="false">AB01</tic:superStar>
            <tic:superStar selected="false">AB02</tic:superStar>
            <tic:superStar selected="false">AB03</tic:superStar>
            <tic:superStar selected="false">AB04</tic:superStar>
        </tic:superStars>
        <tic:validity>1</tic:validity>
        <tic:dateTime>2014-05-20T18:36:28</tic:dateTime>
    </tic:ticket>
    <tic:ticket ticketId="1">
        <tic:plays>
            <tic:play playId="0">
                <tic:numbers>
                    <tic:number>1</tic:number>
                    <tic:number>2</tic:number>
                    <tic:number>3</tic:number>
                    <tic:number>49</tic:number>
                    <tic:number>50</tic:number>
                </tic:numbers>
                <tic:stars>
                    <tic:star>1</tic:star>
                    <tic:star>11</tic:star>
                </tic:stars>
            </tic:play>
        </tic:plays>
        <tic:superStars>
            <tic:superStar selected="false">AB01</tic:superStar>
            <tic:superStar selected="false">AB02</tic:superStar>
            <tic:superStar selected="false">AB03</tic:superStar>
            <tic:superStar selected="false">AB04</tic:superStar>
        </tic:superStars>
        <tic:validity>6</tic:validity>
        <tic:dateTime>2014-05-19T09:44:53</tic:dateTime>
    </tic:ticket>
</tic:tickets>

架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:tic="http://www.ti.bfh.ch/i1p/akz/schema/tickets"
           targetNamespace="http://www.ti.bfh.ch/i1p/akz/schema/tickets" elementFormDefault="qualified">

    <xs:element name="tickets">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
                <xs:element name="ticket">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="tic:ticketType">
                                <xs:attribute name="ticketId" type="xs:integer" use="required"/>
                            </xs:extension>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="uniqueTicketId">
            <xs:selector xpath="tic:ticket"/>
            <xs:field xpath="@ticketId"/>
        </xs:unique>
    </xs:element>


    <xs:complexType name="ticketType">
        <xs:sequence>
            <xs:element name="plays">
                <xs:complexType>
                    <xs:sequence maxOccurs="7" minOccurs="1">
                        <xs:element name="play">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="numbers">
                                        <xs:complexType>
                                            <xs:sequence maxOccurs="5" minOccurs="5">
                                                <xs:element name="number" type="tic:numberType"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                        <xs:unique name="uniqueNumber">
                                            <xs:selector xpath="tic:number"/>
                                            <xs:field xpath="."/>
                                        </xs:unique>
                                    </xs:element>
                                    <xs:element name="stars">
                                        <xs:complexType>
                                            <xs:sequence maxOccurs="2" minOccurs="2">
                                                <xs:element name="star" type="tic:starType"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                        <xs:unique name="uniqueStar">
                                            <xs:selector xpath="tic:star"/>
                                            <xs:field xpath="."/>
                                        </xs:unique>
                                    </xs:element>
                                </xs:sequence>
                                <xs:attribute name="playId" type="xs:integer" use="required"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
                <xs:unique name="uniquePlayId">
                    <xs:selector xpath="tic:play"/>
                    <xs:field xpath="@playId"/>
                </xs:unique>
            </xs:element>
            <xs:element name="superStars">
                <xs:complexType>
                    <xs:sequence maxOccurs="4" minOccurs="4">
                        <xs:element name="superStar">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="tic:superStarType">
                                        <xs:attribute default="false" name="selected"
                                                      type="xs:boolean" use="optional"/>
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
                <xs:unique name="uniqueSuperStar">
                    <xs:selector xpath="tic:superStar"/>
                    <xs:field xpath="."/>
                </xs:unique>
            </xs:element>
            <xs:element name="validity" type="tic:validityType"/>
            <xs:element name="dateTime" type="xs:dateTime"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="validityType">
        <xs:restriction base="xs:integer">
            <xs:enumeration value="1"/>
            <xs:enumeration value="2"/>
            <xs:enumeration value="4"/>
            <xs:enumeration value="6"/>
            <xs:enumeration value="8"/>
            <xs:enumeration value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="numberType">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="50"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="starType">
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="1"/>
            <xs:maxInclusive value="11"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="superStarType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Z0-9][A-Z0-9][A-Z0-9][A-Z0-9]"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema>