Page 1 of 1

dependent child elements with xsd:assert

Posted: Sat Oct 20, 2018 1:57 pm
by franciscator
Hey there,

I have a question for my usage of xsd:assert with xsd 1.1

I'd like to give you a minimum example of what i need to do:

xsd i want to build

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

<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="available" type="xsd:boolean"/>
<xsd:element name="aa" minOccurs="0"/>
<xsd:element name="bb" minOccurs="0"/>
<xsd:element name="cc" minOccurs="0"/>
</xsd:sequence>
<xsd:assert test="(available[. eq true] and aa and bb and cc) or available[. eq false]"/>
</xsd:complexType>
</xsd:element>

</xsd:schema>
[/Codebox]

xsi i want to have NOT valid

[Codebox=]
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<available>true</available>

</root>
[/Codebox]

xsi i want to have valid

[Codebox=]
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<available>false</available>

</root>
[/Codebox]


So i want to have the child elements "aa" "bb" and "cc" required depending on if "available" is set on true or not.
This works pretty well by using data-type "integer" for "available". If i set first condition to "available[. eq 1] ...." and second to "available[. ne 1]" then following xsd is valid and instance works like its meant to be:

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

<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="available" type="xsd:integer"/>
<xsd:element name="aa" minOccurs="0"/>
<xsd:element name="bb" minOccurs="0"/>
<xsd:element name="cc" minOccurs="0"/>
</xsd:sequence>
<xsd:assert test="(available[. eq 1] and aa and bb and cc) or available[. ne 1]"/>
</xsd:complexType>
</xsd:element>

</xsd:schema>
[/Codebox]

and referring instance documents (valid):

[Codebox=]
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<available>1</available>
<aa/>
<bb/>
<cc/>
</root>
[/Codebox]

[Codebox=]
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<available>0</available>
</root>
[/Codebox]



____________________________________________________________________________________________________________

So, okay, i can deal with the "available"-element as an integer, but as the instance document will be written by software afterwards i would like much more, if child-elements could be depending on a boolean element for validation...
Has anybody an idea, why this doesn't work out with this data type?

thank you so much in advance,


kind regards

Franze

Re: dependent child elements with xsd:assert

Posted: Mon Oct 22, 2018 4:09 pm
by tavy
Hello Franze,
I think that you need to change the assert and use a string value for true and false , something like this:

Code: Select all


<xsd:assert test="(available[string(.) eq 'true'] and aa and bb and cc) or available[string(.) eq 'false']"/>
Best Regards,
Octavian