1

I have the following XSD (part of the XSD)

            <xs:element name="sourceValue" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:normalizedString">
                            <xs:attribute name="label" type="xs:normalizedString" 
                                          use="required"/>  
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>

In my XML I have:

<?xml version="1.0" encoding="UTF-8"?>
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="assertion.xsd">
  <SSN>33333332</SSN>
  <sourceValue label="nrA">33333333</sourceValue>
  <sourceValue label="nrB">111111111</sourceValue>
  <Data>
    <Patient>
      <DateOfBirth>03-04-2000</DateOfBirth>
      <Sexe>M</Sexe>
      <Name>Patient A</Name>
    </Patient>
  </Data>
</Record>

I want to change my XSD in such a way that when sourceValue with label="nrA" is mandatory, but with label="nrB" is optional. But I cannot figure out how to do this.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Stephan
  • 463
  • 2
  • 7
  • 22
  • Can you give a sample of a valid and a sample of an invalid document? Also which version of the schema language do you want to use? In the current version 1.1 you can use assertions with XPath which gives you much more expressiveness than 1.0 has. – Martin Honnen Jul 12 '15 at 12:41
  • Also, *what* is optional or mandatory based upon the value of `label`? It sounds like you're saying that the requiredness of the `sourceValue` element itself should depend upon the value of one of its attributes, but that would not make sense. – kjhughes Jul 12 '15 at 12:49
  • Hi @Martin Honnen, version is 1.0. – Stephan Jul 12 '15 at 13:24
  • @kjhughes, you are correct. Both labels are present, but I want to have the first sourceValue with label="nrA" to be mandatory. the complete xml sample looks like 'code' 33333332 33333333 111111111 03-04-2000 M Patient A 'code – Stephan Jul 12 '15 at 13:24
  • @Stephan: Use xsd-1.0 tag when you cannot use xsd-1.1, and add XML to question, not in comments. I've done both for you this time. You cannot specify such constraints in XSD 1.0; see [my answer for further details](http://stackoverflow.com/a/31371411/290085). – kjhughes Jul 12 '15 at 19:07

1 Answers1

2

XSD 1.0

Not possible. Instead, you should use different element names for the two cases.

XSD 1.1

Different element names are still recommended, but if you must follow your current approach, you can use assertions:

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

  <xs:element name="Record">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="SSN" type="xs:string"/>
        <xs:element ref="sourceValue"/>
        <xs:element ref="sourceValue" minOccurs="0"/>
        <xs:element name="Data">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Patient">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="DateOfBirth" type="xs:string"/>
                    <xs:element name="Sexe" type="xs:string"/>
                    <xs:element name="Name" type="xs:string"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:assert test="sourceValue[1]/@label = 'nrA'"/>
      <xs:assert test="not(sourceValue[2]) or sourceValue[2]/@label = 'nrA'"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="sourceValue">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="label" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks you, unfortunately I'm restricted to this xml as is. So in my case it is just not possible. I did try using different element-names, but than the xml file was not process correctly in the application we are using. Otherwise that would be the best option indeed, to use different element-names. – Stephan Jul 12 '15 at 22:23