0

我有一个如下的 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>元素具有一个称为“标识符”的唯一属性。

我想要一个这样的 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>
4

1 回答 1

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>
于 2013-04-22T12:06:48.607 回答