1

I'm trying to accomplish the following, here is my XML document :

<TEST>
  <A>X</A>
  <B>X</B>
  <C>Y</C>
</TEST>

All the three element A, B and C must exist and only one of them must have Y value, the others must have X then.

I used the following code but apparently it's not working :

<xsd:complexType name="TEST">
    <xsd:choice>
        <xsd:sequence>
            <xsd:element name="A" type="xsd:string" fixed="Y"/>
            <xsd:element name="B" type="xsd:string" fixed="X"/>
            <xsd:element name="C" type="xsd:string" fixed="X"/>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="A" type="xsd:string" fixed="X"/>
            <xsd:element name="B" type="xsd:string" fixed="Y"/>
            <xsd:element name="C" type="xsd:string" fixed="X"/>
        </xsd:sequence>
        <xsd:sequence>
            <xsd:element name="A" type="xsd:string" fixed="X"/>
            <xsd:element name="B" type="xsd:string" fixed="X"/>
            <xsd:element name="C" type="xsd:string" fixed="Y"/>
        </xsd:sequence>
    </xsd:choice>
</xsd:complexType>

I've been stuck with this for a while, tried almost everything from asserts, alternatives to restrictions but nothing worked so desperately I tried the choice statement. Hopefully someone gets what I'm trying to do and explain how to accomplish it.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
mrbm
  • 2,164
  • 12
  • 11

1 Answers1

0

You cannot represent such a constraint within XSD 1.0. Your attempt using xs:choice violates Unique Particle Attribution. If you're limited to XSD 1.0, you'll have to enforce that constraint outside of XSD.

XSD 1.1 Solution

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
  elementFormDefault="qualified" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">

  <xs:element name="TEST">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="A" type="yorn"/>
        <xs:element name="B" type="yorn"/>
        <xs:element name="C" type="yorn"/>
      </xs:sequence>
      <xs:assert test="count(* = 'Y') = 1"/>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="yorn">
    <xs:restriction base="xs:token">
      <xs:enumeration value="Y"/>
      <xs:enumeration value="N"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I'm not limited to XSD 1.0, as mentioned in my question I already tried the assert statement but it's not working, can you point me to a tool to test the following XSD ? I think I'm using a XSD 1.0 validator to validate XSD 1.1 , Thanks – mrbm Jan 12 '15 at 19:17
  • I only meant to save you the trouble of trying other XSD 1.0 approaches such as the one you mentioned involving `xsd:choice`. I also did not know how you used `xsd:assert`, which is easy to get wrong, so I included an example. Processors that support XSD 1.1 are listed [**here**](http://stackoverflow.com/questions/19809141/is-it-possible-to-validate-an-xml-file-against-xsd-1-1-in-python/19818871#19818871). – kjhughes Jan 12 '15 at 19:28