XS-Schema独特元素

时间:2012-10-18 19:59:17

标签: xml xsd

我试图创建一个独特的元素。这是我的代码的一部分。

<complexType name="DeliveryOrder">
    <sequence>
        <element name="Depot" type="tns:County" minOccurs="1" maxOccurs="1"></element>
        <element name="CustomerDetails" type="tns:CustomerDetails" minOccurs="1" maxOccurs="1"></element>
        <element name="ItemsToBeDelivered"
            type="tns:ItemsToBeDelivered" maxOccurs="unbounded" minOccurs="1">
        </element>
    </sequence>
</complexType>

<complexType name="CustomerDetails">
    <sequence>
        <element name="FirstName">
            <simpleType>
                <restriction base="string">
                    <pattern value="[a-zA-Z][a-z][a-z]*"></pattern>
                </restriction>
            </simpleType>
        </element>
        <element name="Surname">
            <simpleType>
                <restriction base="string">
                    <pattern value="[a-z{^}A-Z][a-z][a-z]*"></pattern>
                </restriction>
            </simpleType>
        </element>
        <element name="AddressLine1">
            <simpleType>
                <restriction base="string">
                    <minLength value="4"></minLength>
                    <whiteSpace value="collapse"></whiteSpace>
                </restriction>
            </simpleType>
        </element>
        <element name="AddressLine2">
            <simpleType>
                <restriction base="string">
                    <minLength value="4"></minLength>
                    <whiteSpace value="collapse"></whiteSpace>
                </restriction>
            </simpleType>
        </element>
        <element name="AddressLine3" type="string">
        </element>
        <element name="County" type="tns:County" maxOccurs="1" minOccurs="1"></element>
    </sequence>
</complexType>

<complexType name="County">
    <choice>
        <element name="CountyName">
            <simpleType>
                <restriction base="string">
                    <enumeration value="Dublin"></enumeration>
                    <enumeration value="Cork"></enumeration>
                    <enumeration value="Kildare"></enumeration>
                    <enumeration value="Waterford"></enumeration>
                    <enumeration value="Wicklow"></enumeration>
                </restriction>
            </simpleType>
        </element>
    </choice>
</complexType>

<complexType name="DaysForDelivery">
    <sequence>
        <element name="Day">
            <simpleType>
                <restriction base="string">
                    <enumeration value="Monday"></enumeration>
                    <enumeration value="Tuesday"></enumeration>
                    <enumeration value="Wednesday"></enumeration>
                    <enumeration value="Thursday"></enumeration>
                    <enumeration value="Friday"></enumeration>
                </restriction>
            </simpleType>
        </element>
    </sequence>
</complexType>

<element name="DeliveryOrder" type="tns:DeliveryOrder"></element>

<complexType name="ItemsToBeDelivered">
    <sequence>
        <element name="DaysForDelivery" type="tns:DaysForDelivery"
            maxOccurs="unbounded" minOccurs="1">
        </element>
        <element name="Item" type="tns:Item" maxOccurs="unbounded" minOccurs="1"></element>
    </sequence>
</complexType>

<complexType name="Item">
    <sequence>
        <element name="Description">
            <simpleType>
                <restriction base="string">
                    <enumeration
                        value="Regular Milk - 1 litre carton.">
                    </enumeration>
                    <enumeration
                        value="Low Fat Milk - 1 litre carton.">
                    </enumeration>
                    <enumeration
                        value="Strawberry Yoghurt - 100ml pot">
                    </enumeration>
                    <enumeration
                        value="Raspberry Yoghurt - 100ml pot">
                    </enumeration>
                    <enumeration value="Cream - 125ml carton"></enumeration>
                    <enumeration value="Cream - 250ml carton"></enumeration>
                </restriction>
            </simpleType>
        </element>
        <element name="Quantity">
            <simpleType>
                <restriction base="int">
                    <minInclusive value="1"></minInclusive>
                </restriction>
            </simpleType>
        </element>
    </sequence>
</complexType>

所以,我想要的元素&#34; Day&#34;来自&#34; DaysForDelivery&#34;独一无二。 例如:我可以在星期一有一个X项目,之后再也不能使用星期一。 我知道我必须使用唯一标记,并且我看了很多例子,但是我把它弄乱了xpath。

提前谢谢

1 个答案:

答案 0 :(得分:1)

更新:这是修改后的XSD:

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:tns="http://tempuri.org/XMLSchema.xsd" xmlns="http://www.w3.org/2001/XMLSchema">
    <complexType name="DeliveryOrder">
        <sequence>
            <element name="Depot" type="tns:County" minOccurs="1" maxOccurs="1"></element>
            <element name="CustomerDetails" type="tns:CustomerDetails" minOccurs="1" maxOccurs="1"></element>
            <element name="ItemsToBeDelivered" type="tns:ItemsToBeDelivered" maxOccurs="unbounded" minOccurs="1">
                <unique name="UQ">
                    <selector xpath="tns:DaysForDelivery/tns:Day"/>
                    <field xpath="."/>
                </unique>
            </element>
        </sequence>
    </complexType>
    <complexType name="CustomerDetails">
        <sequence>
            <element name="FirstName">
                <simpleType>
                    <restriction base="string">
                        <pattern value="[a-zA-Z][a-z][a-z]*"></pattern>
                    </restriction>
                </simpleType>
            </element>
            <element name="Surname">
                <simpleType>
                    <restriction base="string">
                        <pattern value="[a-z{^}A-Z][a-z][a-z]*"></pattern>
                    </restriction>
                </simpleType>
            </element>
            <element name="AddressLine1">
                <simpleType>
                    <restriction base="string">
                        <minLength value="4"></minLength>
                        <whiteSpace value="collapse"></whiteSpace>
                    </restriction>
                </simpleType>
            </element>
            <element name="AddressLine2">
                <simpleType>
                    <restriction base="string">
                        <minLength value="4"></minLength>
                        <whiteSpace value="collapse"></whiteSpace>
                    </restriction>
                </simpleType>
            </element>
            <element name="AddressLine3" type="string">
            </element>
            <element name="County" type="tns:County" maxOccurs="1" minOccurs="1"></element>
        </sequence>
    </complexType>

    <complexType name="County">
        <choice>
            <element name="CountyName">
                <simpleType>
                    <restriction base="string">
                        <enumeration value="Dublin"></enumeration>
                        <enumeration value="Cork"></enumeration>
                        <enumeration value="Kildare"></enumeration>
                        <enumeration value="Waterford"></enumeration>
                        <enumeration value="Wicklow"></enumeration>
                    </restriction>
                </simpleType>
            </element>
        </choice>
    </complexType>

    <complexType name="DaysForDelivery">
        <sequence>
            <element name="Day">
                <simpleType>
                    <restriction base="string">
                        <enumeration value="Monday"></enumeration>
                        <enumeration value="Tuesday"></enumeration>
                        <enumeration value="Wednesday"></enumeration>
                        <enumeration value="Thursday"></enumeration>
                        <enumeration value="Friday"></enumeration>
                    </restriction>
                </simpleType>
            </element>
        </sequence>
    </complexType>

    <element name="DeliveryOrder" type="tns:DeliveryOrder"></element>

    <complexType name="ItemsToBeDelivered">
        <sequence>
            <element name="DaysForDelivery" type="tns:DaysForDelivery" maxOccurs="unbounded" minOccurs="1">
            </element>
            <element name="Item" type="tns:Item" maxOccurs="unbounded" minOccurs="1"></element>
        </sequence>
    </complexType>

    <complexType name="Item">
        <sequence>
            <element name="Description">
                <simpleType>
                    <restriction base="string">
                        <enumeration value="Regular Milk - 1 litre carton.">
                        </enumeration>
                        <enumeration value="Low Fat Milk - 1 litre carton.">
                        </enumeration>
                        <enumeration value="Strawberry Yoghurt - 100ml pot">
                        </enumeration>
                        <enumeration value="Raspberry Yoghurt - 100ml pot">
                        </enumeration>
                        <enumeration value="Cream - 125ml carton"></enumeration>
                        <enumeration value="Cream - 250ml carton"></enumeration>
                    </restriction>
                </simpleType>
            </element>
            <element name="Quantity">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="1"></minInclusive>
                    </restriction>
                </simpleType>
            </element>
        </sequence>
    </complexType>
</schema>

enter image description here

示例有效 XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<DeliveryOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
    <Depot>
        <CountyName>Dublin</CountyName>
    </Depot>
    <CustomerDetails>
        <FirstName>Firstname</FirstName>
        <Surname>Surname</Surname>
        <AddressLine1>AddressLine11</AddressLine1>
        <AddressLine2>AddressLine21</AddressLine2>
        <AddressLine3>AddressLine31</AddressLine3>
        <County>
            <CountyName>Dublin</CountyName>
        </County>
    </CustomerDetails>
    <ItemsToBeDelivered>
        <DaysForDelivery>
            <Day>Monday</Day>
        </DaysForDelivery>
        <DaysForDelivery>
            <Day>Tuesday</Day>
        </DaysForDelivery>
        <Item>
            <Description>Regular Milk - 1 litre carton.</Description>
            <Quantity>1</Quantity>
        </Item>
        <Item>
            <Description>Regular Milk - 1 litre carton.</Description>
            <Quantity>1</Quantity>
        </Item>
    </ItemsToBeDelivered>
</DeliveryOrder>

如果您将Tuesday更改为Monday,则会获得:

Error occurred while loading [], line 22 position 17
There is a duplicate key sequence 'Monday' for the 'http://tempuri.org/XMLSchema.xsd:UQ' key or  unique identity constraint.
Document1.xml is invalid.