0

Given the following XML:

<category>1000</category>
<type>a</type>

And this XSD for validation:

<xs:element name="category" type="categoryType"/>
<xs:element name="type" type="xs:string">

<xs:simpleType name="categoryType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="1000"/>
    <xs:enumeration value="1010"/>
    <xs:enumeration value="1020"/>
    <xs:enumeration value="1030"/>
    <xs:enumeration value="1040"/>
  </xs:restriction>
</xs:simpleType>

How can I check the type value if:

  • type = a (avaliable in category: all)
  • type = b (avaliable in category: 1000,1020,1030)
  • type = c (avaliable in category: 1000,1010,1030,1040)
  • type = d (avaliable in category: 1000,1010,1020,1030)

1 Answers1

0

XSD 1.0

Not possible without redesigning your XML such that type differentiation is conveyed via element name rather than a @type attribute.

XSD 2.0

Define new types via restrictions on categoryType for each of b, c, d, then use Conditional Type Assignment to allow type to vary per @type.

See also: How to make type depend on attribute value using Conditional Type Assignment

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • In this case `type` is a child element rather than an attribute, so conditional type assignment won't work; it has to be done using assertions. – Michael Kay Oct 10 '18 at 14:39
  • @MichaelKay: Oh, OP does indeed show it as an element. I suppose, short of XML redesign, that leaves `xs:assert` or out-of-band wrt XSD checking of the constraint. Thanks. – kjhughes Oct 10 '18 at 15:01