I have tried assert in XSD and I get the error
Element 'assert' Is Invalid, Misplaced, Or Occurs Too Often.
My example is the below XML.
`<?xml version="1.0" encoding="utf-8"?>
<p:CustomerElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<recordCount>1234</recordCount>
<Customer>
<indicator>A</indicator>
<test1>hdjfs</test1>
<test2>idsfh</test2>
<test3>idsfh</test3>
<test4>idsfh</test4>
<test5>idsfh</test5>
<test6>idsfh</test6>
<test7>idsfh</test7>
</Customer>
<Customer>
<indicator>D</indicator>
<test1>abcd</test1>
<test3>jhf</test3>
</Customer>
</p:CustomerElement>`
The XSD that I created for this is
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://My.Schema.Namespace"
targetNamespace="http://My.Schema.Namespace"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">>
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="indicator">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="A" />
<xs:enumeration value="B" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:choice>
<xs:element name="test1" />
<xs:element name="test2" />
<xs:element name="test3" />
<xs:element name="test4" />
<xs:element name="test5" />
<xs:element name="test6" />
<xs:element name="test7" />
</xs:choice>
</xs:sequence>
<xs:assert test="if(indicator eq 'A') then test1 and test2 and test3 and test4
and test5 and test6 and test7
else if(indicator eq 'B') then test1 and test3"/>
</xs:complexType>
</xs:element>
</xs:schema>
I am validating this above xml with XSD in freeformat validator.
There are few errors in the syntax of assert i guess. Can anyone please help on the condition i was looking for and help me also on syntax and also let me know if i have added the correct links that support XSD 1.1 schema.
To resolve the problems with your XSD:
Change
elementFormDefault="qualified"
vc:minVersion="1.1">>
to
vc:minVersion="1.1">
to eliminate the spurious >
and the duplicated elementFormDefault
attribute.
Add an else false()
to your assertion test so that it is well-formed. Better yet, re-write to the logical equivalent without the compound if-else
construction.
Use an XSD 1.1 processor if you wish to use xs:assert
. Freeformat.com only supports XSD 1.0.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://My.Schema.Namespace"
targetNamespace="http://My.Schema.Namespace"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="customer">
<xs:complexType>
<xs:sequence>
<xs:element name="indicator">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="A" />
<xs:enumeration value="B" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:choice>
<xs:element name="test1" />
<xs:element name="test2" />
<xs:element name="test3" />
<xs:element name="test4" />
<xs:element name="test5" />
<xs:element name="test6" />
<xs:element name="test7" />
</xs:choice>
</xs:sequence>
<xs:assert test="if (indicator eq 'A')
then test1 and test2 and test3 and test4 and test5 and test6 and test7
else if (indicator eq 'B') then test1 and test3 else false()"/>
</xs:complexType>
</xs:element>
</xs:schema>
Achieving validity of your XML against this XSD is left as an exercise for the asker to complete, assuming the above obstacles can be overcome.