XML - 具有子元素的属性和序列的模式元素

时间:2013-04-22 11:32:51

标签: xml xsd

我有一个XML架构如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:element name="labels">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="label"  minOccurs="1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="language"  minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute name="value" type="xs:string" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>    

</xs:schema>

<labels>可以包含多个<label>元素,而<label>元素可以包含多个<language>元素。现在我需要的是<label>元素具有一个名为'identifier'的唯一属性。

我希望有这样的XML结构:

<?xml version="1.0" encoding="UTF-8"?>

<labels
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xsi:noNamespaceSchemaLocation='labels.xsd'>

    <label identifier="class_contact">
        <language value="english">Contacts</language>
        <language value="afrikaans">Kontakte</language>
    </label>

</labels>

好的我把它改成了这个,现在它允许标识符属性,但它并不强制它是唯一的。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:element name="labels">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="label" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="language"  minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute name="value" type="xs:string" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="identifier" type="xs:string" />
                    </xs:complexType>
                    <xs:unique name="UniqueLabelLanguage">
                        <xs:selector xpath="language" /> 
                        <xs:field xpath="@value" /> 
                    </xs:unique>
                    <xs:unique name="UniqueLabelIdentifier">
                        <xs:selector xpath="label" /> 
                        <xs:field xpath="@identifier" /> 
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>    

</xs:schema>

1 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:element name="labels">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="label" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="language"  minOccurs="1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:simpleContent>
                                        <xs:extension base="xs:string">
                                            <xs:attribute name="value" type="xs:string" />
                                        </xs:extension>
                                    </xs:simpleContent>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="identifier" type="xs:string" />
                    </xs:complexType>
                    <xs:unique name="UniqueLabelLanguage">
                        <xs:selector xpath="language" /> 
                        <xs:field xpath="@value" /> 
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="UniqueLabelIdentifier">
            <xs:selector xpath="label" /> 
            <xs:field xpath="@identifier" /> 
        </xs:unique>
    </xs:element>    

</xs:schema>