Problem with XSD schema

This should cover W3C XML Schema, Relax NG and DTD related problems.
Posts: 1
Joined: Tue Jul 11, 2006 12:40 pm

Problem with XSD schema

Post by ratters » Tue Jul 11, 2006 12:42 pm

I have to elements that can appear in a XML file i recieve.

This would be valid


So would this be valid


This would not be valid


Also it should be valid for both not to appear at all in the XML file who can I achieve this ??


Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Jul 11, 2006 2:47 pm


Having a dependence in the content of b on the content of a is called co-occurance constrait and it is not supported by XML Schema. Also XML Schema does not allow ambiguous content. You need to declare a more relaxed schema and enforce the constraint at some other level, for instance using Schematron embedded rules. You can also use Relax NG schema as that allows handling this situation.
oXygen supports both XML Schema with Schematron embedded rules and Relax NG schema so you can use either solution with oXygen. Example Relax NG and XML Schema with embedded Schematron rules are below.

Relax NG in XML syntax

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="">
<element name="test">
<element name="a">
<element name="b">
<element name="a">
<element name="b">
Relax NG in compact syntax

Code: Select all

default namespace = ""

start =
element test {
((element a { empty },
element b { empty })
| (element a { "data" },
element b { "data" }))?
XML Schema with embedded Schematron rules

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs=""
<xs:element name="test">
<xs:sequence minOccurs="0">
<xs:element ref="a"/>
<xs:element ref="b"/>
<xs:element name="a" type="dataOrEmpty">
<pattern xmlns="" name="test">
<rule context="a[not(text())]">
<assert test="not(following-sibling::b/text())">a is empty
then b should also be empty.</assert>
<rule context="a[text()]">
<assert test="text()=following-sibling::b/text()">a and b
should have the same value</assert>
<xs:element name="b" type="dataOrEmpty"/>
<xs:simpleType name="dataOrEmpty">
<xs:restriction base="xs:string">
<xs:enumeration value="data"/>
<xs:enumeration value=""/>
All the above schemas work with oXygen. The input documents should have a test root element and optionally a and b elements, either empty or both with data as content in order to have valid instances.

Best Regards,

Post Reply