3

I have XML (as an example) with a segment called Type within the segment Order. The segment is a one to many, so we have something like :

<Order>
  <Type>1</Type>
  <Type>2</Type>
  <Type>3</Type>
</Order>

Now the validation should be, that there always should be segment Type with value 1 in the segment Order, if not, a validation error should occur. I tried some validations, but could not get the correct result. Has someone thought on how to implement this and if this is even possible?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
user3756056
  • 51
  • 1
  • 3

2 Answers2

2

XSD 1.0

Your constraint cannot be expressed in XSD 1.0.

XSD 1.1

Your constraint can be expressed in XSD 1.1 using an assertion to state that there be at least one Type child of Order that has a value of 1:

<?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="Order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Type" maxOccurs="unbounded" type="xs:integer"/>
      </xs:sequence>
      <xs:assert test="Type = 1"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for the clear answer, it helped me a lot with implementing the correct solution for this case and also know the bounderies of the XSD validation within SQL. – user3756056 Apr 18 '16 at 17:23
  • You're welcome. Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer if it's helped. Thanks. – kjhughes Apr 18 '16 at 17:33
1

in XSD 1.1, you can use xs:assertelement:

<xs:element name="Order">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="Type" type="xs:string"/>

        </xs:sequence>
        <xs:assert test="count(Type[text() = '1']) > 0"/>
    </xs:complexType>

</xs:element>
Kachna
  • 2,921
  • 2
  • 20
  • 34
  • Thanks for your quick answer, I use the validation through SQL server which seems to only support 1.0 at this stage. Do you if this can be done from with XSD 1.0? – user3756056 Mar 25 '16 at 12:30