XML Schema <xs:unique/> scope

C

cedric.louyot

Hi,

I've written a schema that looks like :

<xs:schema>
<xs:complexType name="myType">
<xs:sequence>
<xs:element name="e1" type="T1" maxOccurs="unbounded"/>
<xs:element name="e2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T1">
<xs:sequence>
<xs:element name="e3" type="T3" maxOccurs="unbounded"/>
<xs:element name="e4" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T3">
<xs:sequence>
<xs:element name="name" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="adress" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="myElement" type="myType">
<xs:unique name="uniqueName>
<xs:selector xpath="./e1/e3"/>
<xs:field xpath="name"/>
</xs:unique>
</xs:element>
</xs:schema>

The <xs:unique/> element garantees there is only one element "./e1/e3"
in my xml file that has a given name. This prevents me from having 2
elements with the same name in 2 different e1 elements because my
<xs:unique/> element is in the scope of "myElement". I would like to
be allowed to name two element e3 with the same name providing they do
not have the same e1 ancestor. Therefore I have to define my
<xs:unique/> element in the scope of "myElement/e1" instead of
"myElement".
How do I do that given that I want to keep defining all my complex
types before defining the "myElement" element ? I would like to avoid
defining more than one <xs:element/> element.

Thanks,

Cédric
 
B

Boris Kolpackov

Hi Cédric,

<xs:schema>
<xs:complexType name="myType">
<xs:sequence>
<xs:element name="e1" type="T1" maxOccurs="unbounded"/>
<xs:element name="e2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T1">
<xs:sequence>
<xs:element name="e3" type="T3" maxOccurs="unbounded"/>
<xs:element name="e4" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="T3">
<xs:sequence>
<xs:element name="name" type="xs:string" maxOccurs="unbounded"/>
<xs:element name="adress" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="myElement" type="myType">
<xs:unique name="uniqueName>
<xs:selector xpath="./e1/e3"/>
<xs:field xpath="name"/>
</xs:unique>
</xs:element>
</xs:schema>

Therefore I have to define my <xs:unique/> element in the scope
of "myElement/e1" instead of "myElement".

How do I do that given that I want to keep defining all my complex
types before defining the "myElement" element ? I would like to avoid
defining more than one <xs:element/> element.

You would just add unique to the e1 element declaration:

<xs:complexType name="myType">
<xs:sequence>
<xs:element name="e1" type="T1" maxOccurs="unbounded">
<xs:unique name="uniqueName>
<xs:selector xpath="e3"/>
<xs:field xpath="name"/>
</xs:unique>
</xs:element>
<xs:element name="e2" type="xs:string"/>
</xs:sequence>
<xs:complexType>


hth,
-boris
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top