XML Schema

N

Nichino

Hello,
I'm trying to define my first Xml Schema and have a problem.
The XML I'd like to define through the Schema is the following one.
Can you help me please? I don't know how to insert the 3 final notes.
Many thanks and my apologies for the trouble.

Best,
Nick

<language>
<combinations>
<combination>
<element> .... </element>
<element> .... </element>
</combination>
<combination>
<element> .... </element>
<element> .... </element>
</combination>
<combination>
<element> .... </element>
<element> .... </element>
</combination>
</combinations>

</language>

Note
1. There is no limit to the number of combinations.
2. Two elements having the same content can't be combined.

E.g.

<combination>
<element> pippo </element>
<element> pippo </element>
</combination>

it's not valid.

3. it's not possible to insert two combinations having the same elements.

E.g.

<combinations>
<combination>
<element> pippo </element>
<element> pluto </element>
</combination>
<combination>
<element> pluto </element>
<element> pippo </element>
</combination>
</combinations>

it's not valid.
 
G

George Bina

Hi,

Here it is a schema that implements 1 and 2:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="language">
<xs:complexType>
<xs:sequence>
<xs:element ref="combinations"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="combinations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="combination"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="combination">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="2" maxOccurs="2" ref="element"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueElements">
<xs:selector xpath="element"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>
<xs:element name="element" type="xs:NMTOKEN"/>
</xs:schema>

Point 1 is implemented with maxOccurs="unbounded" in
<xs:element maxOccurs="unbounded" ref="combination"/>
and point 2 is implemented with a unique constraint on the combination
element:
<xs:unique name="uniqueElements">
<xs:selector xpath="element"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>

Point 3 cannot be implemented with a unique constraint because the
XPath used in XML Schema does not allow predicates and one cannot refer
to the first element and the second element in a compound constraint,
that is in a constraint with two fields.
Possible approaches further are to embed Schematron rules to check that
constraint
....
<xs:element name="combination">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron"
name="uniquePairTest">
<rule context="combination">
<assert
test="not(preceding::combination[element[1]=current()/element[1] and
element[2]=current()/element[2]])">
We should not have duplicate combinations.
</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
....

or to use different names for the first element and for the second
element, for instance element1 and element2:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="language">
<xs:complexType>
<xs:sequence>
<xs:element ref="combinations"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="combinations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="combination"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueCombination">
<xs:selector xpath="combination"></xs:selector>
<xs:field xpath="element1"/>
<xs:field xpath="element2"/>
</xs:unique>
</xs:element>
<xs:element name="combination">
<xs:complexType>
<xs:sequence>
<xs:element ref="element1"/>
<xs:element ref="element2"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueElements">
<xs:selector xpath="element1|element2"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:unique>
</xs:element>
<xs:element name="element1" type="xs:NMTOKEN"/>
<xs:element name="element2" type="xs:NMTOKEN"/>
</xs:schema>

Best Regards,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top