Problem with XSD schema

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

Problem with XSD schema

Post by ratters »

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

This would be valid

<a>data</a>
<b>data</a>

So would this be valid

<a/>
<b/>

This would not be valid

<a/>
<b>data</b>

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

THA
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

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="http://relaxng.org/ns/structure/1.0">
<start>
<element name="test">
<optional>
<choice>
<group>
<element name="a">
<empty/>
</element>
<element name="b">
<empty/>
</element>
</group>
<group>
<element name="a">
<value>data</value>
</element>
<element name="b">
<value>data</value>
</element>
</group>
</choice>
</optional>
</element>
</start>
</grammar>
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="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="test">
<xs:complexType>
<xs:sequence minOccurs="0">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="a" type="dataOrEmpty">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" 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>
<rule context="a[text()]">
<assert test="text()=following-sibling::b/text()">a and b
should have the same value</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
</xs:element>
<xs:element name="b" type="dataOrEmpty"/>
<xs:simpleType name="dataOrEmpty">
<xs:restriction base="xs:string">
<xs:enumeration value="data"/>
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
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,
George
Post Reply