Search code examples
xmlxsdxsd-validation

In an XML schema, is there a way to validate that a name of an element is the value of another element


I am rather new to XML/xsd and I want to write an XML schema for XML files that I am using in my university project. This is my issue: I have these two elements ´a´ and ´c´. I want to make sure, that the name of each element in ´c´ is the text of an element ´b´ in ´a´.

<a>
    <b>aa</b>
    <b>bb</b>
    <b>cc</b>
    ...
</a>
<c>
    <aa>.....</aa>
    <bb>.....</bb>
    <cc>.....</cc>
    ...
</c>

Is there any way to do this, or do I have to list all the names manually like this?:

<xs:complexType name="c">
    <xs:sequence>
        <xs:element name="aa" .../>
        <xs:element name="bb" .../>
        <xs:element name="cc" .../>
        ...
    </xs:sequence>
</xs:complexType>

I know that I can use assert to check whether an element exists, but I do not know if I can check that using the value of another element as the name, and I do not know if I can do this for each element ´b´, where the count of ´b´ is arbitrary.

Thanks in advance!


Solution

  • This can't be done readily in XSD 1.0. In XSD 1.1 you can write an assertion (on the common ancestor of the elements you have shown):

    <xs:assert test="every $n in c/* satisfies name($n) = a/b"/>