make field mandatory depending on values
This should cover W3C XML Schema, Relax NG and DTD related problems.
-
- 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
if would be
Thanks Regards Mario
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
Code: Select all
name="title"
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>
-
- Posts: 2879
- Joined: Tue May 17, 2005 4:01 pm
Re: make field mandatory depending on values
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
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
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- 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
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
-
- Posts: 30
- Joined: Wed Feb 14, 2018 3:27 pm
Re: make field mandatory depending on values
Post by mariomueller »
Got it:
Schema:
XML Instance:
Regards Mario
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>
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>
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