XSD key with multiple fields

2.1k Views Asked by At

I have structure like this:

<parent>
    <name>p1</name>
    <child>
        <name>c3</name>
        <name>c1</name>
    </child>
</parent>
<parent>
    <name>p2</name>
    <child>
        <name>c1</name>
        <name>c2</name>
    </child>
</parent>

What I want is to have another section which values should be validated againts first section. So that

<parent name="p1"> 
    <child name="c3" /> 
</parent>

or

<parent name="p2">
    <child name="c2" />
</parent>

should be valid, but neither

 <parent name="p1"> 
    <child name="c2" /> 
 </parent>

nor

<parent name="p2">
    <child name="c4" />
</parent>

I was thinking about using key with multiple fields

<xs:key name="k">
    <xs:selector xpath="tns:parent" />
    <xs:field xpath="tns:name" />
    <xs:field xpath="tns:child/tns:name" />
</xs:key>

but is seems that this approach can never work because key fields should be unique.

Is it possible to do this with XML Schema 1.0?

1

There are 1 best solutions below

0
On

Correct, the multiple child/name elements within the parent definitions are the problem. Too bad XML Schema doesn't allow XPath parent steps, since you could then select on the child name:

<xs:key name="pcdef">
  <xs:selector xpath=".//tns:parent/tns:child/tns:name"/>
  <xs:field xpath="../../tns:name" />
  <xs:field xpath="." />
</xs:key>