Validate XML element attribute value depending on child element attribute

1.2k views Asked by At

I have this in XML:

<generator Mode="High">
    <GenMode name="Normal" kV="90" mA="0.5" />
    <GenMode name="High" kV="160" mA="0.7" />
</generator>

generator/@Mode should be valid only if it matches one of the generator/GenMode/@name. Is it possible with XSD 1.0?

Now I'm using this rules in xsd file for this element:

<xs:element name="generator">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="GenMode" maxOccurs="unbounded" type="GenModeType" />
    </xs:sequence>
    <xs:attribute name="Mode" use="required" type="xs:string"/>
  </xs:complexType>
  <xs:key name="GenModeName">
    <xs:selector xpath="GenMode"/>
    <xs:field xpath="@name"/>
  </xs:key>
</xs:element>
<xs:complexType name="GenModeType">
  <xs:attribute name="name" use="required" type="xs:string"/>
  <xs:attribute name="kV" use="required" type="xs:integer"/>
  <xs:attribute name="mA" use="required" type="xs:decimal"/>
</xs:complexType>
2

There are 2 answers

0
kot-da-vinci On BEST ANSWER

I found the way to validate the attribute value with XSD 1.0 and it should not refer only to the first child element.

<xs:element name="generator">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="GenMode" maxOccurs="unbounded" type="GenModeType"/>
    </xs:sequence>
    <xs:attribute name="Mode" use="required" type="xs:string"/>
  </xs:complexType>
  <xs:unique name="GenModeNameUniquenessCheck">
    <!-- check for uniqueness @name attribute of all GenMode elements -->
    <xs:selector xpath="GenMode"/>
    <xs:field xpath="@name"/>
  </xs:unique>
  <xs:keyref name="GeneratorModeValid" refer="GenModeNameUniquenessCheck">
    <!-- check generator/@Mode is one of generator/GenMode/@name  -->
    <xs:selector xpath="."/>
    <xs:field xpath="@Mode"/>
  </xs:keyref>     
</xs:element>
<xs:complexType name="GenModeType">
  <xs:attribute name="name" use="required" type="xs:string"/>
  <xs:attribute name="kV" use="required" type="xs:integer"/>
  <xs:attribute name="mA" use="required" type="xs:decimal"/>
</xs:complexType>
0
sergioFC On

I think that the closest you can do in XSD 1.0 without creating elements with different names is define that the generator/@Mode should refer to the first GenMode/@name:

<xs:element name="generator">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="GenMode" type="GenModeType">
                <!-- This is the key of the first GenMode -->
                <xs:key name="firstGenModeKey">
                    <xs:selector xpath="."/>
                    <xs:field xpath="@name"/>
                </xs:key>
            </xs:element> 
            <xs:element name="GenMode" minOccurs="0" maxOccurs="unbounded" type="GenModeType" />
        </xs:sequence>
        <xs:attribute name="Mode" use="required" type="xs:string"/>
    </xs:complexType>
    <!-- Mode attribute should refer to firstGenModeKey -->
    <xs:keyref refer="firstGenModeKey" name="ign">
        <xs:selector xpath="."/>
        <xs:field xpath="@Mode"/>
    </xs:keyref>
    <!-- Every genMode/@name should be unique, so you can use xs:unique also -->
    <xs:key name="GenModeName">
        <xs:selector xpath="GenMode"/>
        <xs:field xpath="@name"/>
    </xs:key>
</xs:element>
<xs:complexType name="GenModeType">
    <xs:attribute name="name" use="required" type="xs:string"/>
    <xs:attribute name="kV" use="required" type="xs:integer"/>
    <xs:attribute name="mA" use="required" type="xs:decimal"/>
</xs:complexType>

As you know, if you use XSD 1.1 you only need to add one assertion like this example one:

<xs:assert test="count(GenMode[@name = ../@Mode]) = 1"/>