make field mandatory depending on values

This should cover W3C XML Schema, Relax NG and DTD related problems.
mariomueller
Posts: 30
Joined: Wed Feb 14, 2018 3:27 pm

make field mandatory depending on values

Post by mariomueller »

Hi all,

could anybody plaese provide an example, how to make a parent element mandatory if one of its children has a special value?

e.g. how could I make element

Code: Select all

name="book" = minOccurs = 1
if

Code: Select all

name="title"  
would be

Code: Select all

"The Hobbit"

Code: Select all

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book"
minOccurs="0"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="title" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Thanks Regards Mario
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: make field mandatory depending on values

Post by adrian »

Hi,

AFAIK, this kind of condition is only possible in XSD 1.1 or XSD 1.0+embedded Schematron.
For XSD 1.1 you can use xsd:assert. You can find a discussion about such condition on stackoverflow, Conditional required elements in an XSD

For embedded Schematron you can use sch:assert. More details and an example can be found in our user guide:
XML Schema or RELAX NG with Embedded Schematron Rules

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
mariomueller
Posts: 30
Joined: Wed Feb 14, 2018 3:27 pm

Re: make field mandatory depending on values

Post by mariomueller »

Hi Adrian,

thanks for your reply. As I am just starting with XSD 1.1 the example you gave is to complicated for me. I didn't get it. :(

I need a easy example; but I am not sure what words I should take for googeling :-)

Regards
Mario
mariomueller
Posts: 30
Joined: Wed Feb 14, 2018 3:27 pm

Re: make field mandatory depending on values

Post by mariomueller »

Got it:

Schema:

Code: Select all

<?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" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="PRODUCT" type="ProductType"/>
<xs:complexType name="ProductType">
<xs:sequence>
<xs:element name="number" type="xs:integer"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="size" type="xs:integer" minOccurs="0"/>
</xs:sequence>
<xs:attribute name="dept" type="xs:string"/>
<xs:assert test="if (name/text()='uschi')
then not(size)
else true()"/>
</xs:complexType>
</xs:schema>
XML Instance:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!--if element <name> has value 'uschi' , element <size> is not allowed-->
<!-- if element <size> exists, element <name> value 'uschi' is not allowed-->
<PRODUCT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" dept="XXXXX" xsi:noNamespaceSchemaLocation="XSD_Arbeitsversion.xsd">
<number>0</number>
<name>uschi</name>
<size>0</size>
</PRODUCT>
Regards Mario
Post Reply