dependent child elements with xsd:assert
This should cover W3C XML Schema, Relax NG and DTD related problems.
-
- Posts: 1
- Joined: Sat Oct 20, 2018 1:42 pm
dependent child elements with xsd:assert
Post 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
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
-
- Posts: 387
- Joined: Thu Jul 01, 2004 12:29 pm
Re: dependent child elements with xsd:assert
Hello Franze,
I think that you need to change the assert and use a string value for true and false , something like this:
Best Regards,
Octavian
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']"/>
Octavian
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
<oXygen/> XML Editor
http://www.oxygenxml.com
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service