XML Schema:元素属性为唯一值?

时间:2014-06-24 12:58:52

标签: xml unique altova spy

我正在制作XSD架构来验证从java生成的XML文件。关于天气,我想让valuedate唯一。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="forecast">
<xs:complexType>
<xs:sequence>
<xs:element name="city" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="weather">
<xs:complexType>
<xs:sequence>
    <xs:element name="date" maxOccurs="unbounded">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="maxtemp">
                    <xs:simpleType>
                        <xs:restriction base="xs:int"/>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="mintemp" type="xs:int"/>
                <xs:element name="cloudcover">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:minInclusive value="0"/>
                            <xs:maxInclusive value="100"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="humidity">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:minInclusive value="0"/>
                            <xs:maxInclusive value="100"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="precipitation" type="xs:double"/>
                <xs:element name="windspeed">
                    <xs:simpleType>
                        <xs:restriction base="xs:int">
                            <xs:minInclusive value="0"/>
                            <xs:maxInclusive value="100"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="description">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="Heavy rain"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="value" use="required">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:pattern value="\d\d-\d\d-\d\d"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
        <xs:unique name="dateuq">
            <xs:selector xpath="date"/>
            <xs:field xpath="@value"/>
        </xs:unique>
    </xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="zip" use="required">
<xs:simpleType>
<xs:restriction base="xs:positiveInteger">
<xs:totalDigits value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

这是测试XML文件:

<forecast> 
    <city name="Podgorica" zip="81000">
        <weather>
            <date value="01-01-14">
                <maxtemp>10</maxtemp>
                <mintemp>5</mintemp>
                <cloudcover>40</cloudcover>
                <humidity>20</humidity>
                <precipitation>10.2</precipitation>
                <windspeed>3</windspeed>
                <description>Heavy rain</description>
            </date>
            <date value="01-01-14">
                <maxtemp>10</maxtemp>
                <mintemp>5</mintemp>
                <cloudcover>40</cloudcover>
                <humidity>20</humidity>
                <precipitation>10.2</precipitation>
                <windspeed>3</windspeed>
                <description>Heavy rain</description>
            </date>
        </weather>
    </city>
</forecast>

所以这不应该在这里通过验证,因为value应该是唯一的,为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

您将唯一性约束放在date中,但在该上下文中没有任何意义。您可以在weather上下文(或cityforecast等较高的上下文中声明日期的唯一性约束。

为每个weather保证唯一数据,在weather元素声明中声明它:

<xs:element name="weather">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="date" maxOccurs="unbounded">
                <xs:complexType> 
                 ...
                </xs:complexType>
                <!-- remove it from here -->
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <!-- and place it here -->
    <xs:unique name="dateuq">
        <xs:selector xpath="date"/>
        <xs:field xpath="@value"/>
    </xs:unique>
</xs:element>