Andez
Andez

Reputation: 5848

XSD Schemas - Enumeration based on value in document

This is a right long shot.

If I have a defined element in my XSD, containing two attributes, say NAME and VALUE, is it possible to restrict the VALUE enumeration based on the value in the Name?

For example:

<xsd:complexType name="ConfigDef">
    <xsd:attribute name="NAME" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The name of this Config setting.</xsd:documentation>
        </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The value of this Config setting.</xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
</xsd:complexType>

I don't think it can be done. Can it be done? Is there XSD hacks to get it to work?

Upvotes: 1

Views: 844

Answers (1)

nine9ths
nine9ths

Reputation: 795

You could do this with embedded schematron

<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="ConfigDef">
    <xsd:annotation>
      <xsd:appinfo>
        <sch:pattern name="Test constraints on the ConfigDef element"
          xmlns:sch="http://purl.oclc.org/dsdl/schematron">
          <sch:rule
            context="ConfigDef">
            <sch:assert test="@NAME eq 'foo' and @VALUE = ('bar','baz')">Error message when the assertion condition is broken...</sch:assert>
            <sch:assert test="@NAME eq 'qux' and @VALUE = ('teh','xyz')">Error message when the assertion condition is broken...</sch:assert>
          </sch:rule>
        </sch:pattern>
      </xsd:appinfo>
    </xsd:annotation>
    <xsd:attribute name="NAME" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The name of this Config setting.</xsd:documentation>
      </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The value of this Config setting.</xsd:documentation>
      </xsd:annotation>
    </xsd:attribute>
  </xsd:complexType>
</xsd:schema>

Or you could do this with a Relax Schema

element ConfigDef {
  ((
  attribute NAME {'foo'},
  attribute VALUE {'bar'|'baz'}) |  
  (
  attribute NAME {'qux'},
  attribute VALUE {'teh'|'xyz'}))
}

Upvotes: 2

Related Questions