XSD其中element的值是另一个元素的名称

时间:2015-05-20 15:27:10

标签: xml xsd

我已经多次尝试过谷歌,但它要么不可能,要么我使用的术语不正确。是否可以为以下示例制作XSD:

<inputs>
    <input>
        <name>Test 1</name>
        <valuesTag>values_generated_15Jan2015</valuesTag>
    </input>
<inputs>
<values>
    <values_generated_15Jan2015>
        ...
    </values_generated_15Jan2015>
</values>

这样做的原因是我们对值列表只有几个选择,但是大量输入,其中每个值列表由多个输入使用,并且不重复值将是很好的。 / p>

1 个答案:

答案 0 :(得分:1)

使用评论中解释的XSD 1.1断言来查看此示例。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="inputs" type="inputsType" />
                <xs:element name="values" type="valuesType" />
            </xs:sequence>
            <!-- Ensures that there is the same number of value nodes than input nodes -->
            <xs:assert test="count(values/value) = count(inputs/input)" />
            <!-- Ensures that every value in a valueId node is unique  -->
            <xs:assert test="count(inputs/input/valueId) = count(distinct-values(inputs/input/valueId))" />
            <!-- Ensures that every value in a id attribute is unique  -->
            <xs:assert test="count(values/value) = count(distinct-values(values/value/@id))" />
            <!-- Ensures that all values in id attributes appear in valueId -->
            <xs:assert test="count(values/value[@id = ../../inputs/input/valueId]) = count(values/value)" />
        </xs:complexType>
    </xs:element>

    <xs:complexType name="inputsType">
        <xs:sequence>
            <xs:element name="input" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string" />
                        <xs:element name="valueId" type="xs:string" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="valuesType">
        <xs:sequence>
            <xs:element name="value" maxOccurs="unbounded">
                <xs:complexType>
                    <!-- Content -->
                    <xs:attribute name="id" type="xs:string" use="required" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

</xs:schema>