By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Uniqueness based on Attribute-value

P: n/a
Hi,

Is it possible in Xml to enfore the uniqueness of an element based on his
attribute value.

Say I have this schema :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.egemin.com/Epia/StringResources"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.egemin.com/Epia/StringResources"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="StrResources" type="StrResourcesType"/>

<xs:attribute name="LanguageAttr">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="nl"/>
<xs:enumeration value="nl-BE"/>
<xs:enumeration value="nl-NL/">
</xs:restriction>
</xs:simpleType>
</xs:attribute>

<xs:complexType name="StrResourcesType">
<xs:sequence>
<xs:element name="StrRes" type="StrResType" minOccurs="0"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="StrResType">
<xs:sequence>
<xs:element name="Tag" type="xs:ID"/>
<xs:element name="NeutrVal" type="StrValType"/>
<xs:element name="Localized" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Val" type="StrValType"/>
</xs:sequence>
<xs:attribute ref="LanguageAttr" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

</xs:schema>

And I have the following xml :

<?xml version="1.0" encoding="UTF-8"?>
<EPIA:StrResources xmlns:EPIA="http://www.egemin.com/Epia/StringResources"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.egemin.com/Epia/StringResources
StringResources.xsd">
<EPIA:StrRes>
<EPIA:Tag>
Egemin.EPIA.BaseContext.Context
</EPIA:Tag>
<EPIA:NeutrVal>Context</EPIA:NeutrVal>
<EPIA:Localized EPIA:LanguageAttr="nl"> <===
<EPIA:Val>Context</EPIA:Val>
</EPIA:Localized>
<EPIA:Localized EPIA:LanguageAttr="nl"> <===
<EPIA:Val>Contexte</EPIA:Val>
</EPIA:Localized>
</EPIA:StrRes>
</EPIA:StrResources>

How can I enfore that it is not allowed to have the attribute-val "nl" twice
in the same <EPIA:StrRes>-element ?


Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Dirk Declercq wrote:
Is it possible in Xml to enfore the uniqueness of an element based on his
attribute value.
Sure. Make sure you've read XML Schema Primer on the topic:
http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness

This schema should be a bit modified to be able to express uniqueness
constraints. Uniqueness in schema relies on XPath patterns and XPath
doesn't support default namespace, which is your schema is based on. So
additionally declare prefix for your namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.egemin.com/Epia/StringResources"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.egemin.com/Epia/StringResources"
elementFormDefault="qualified" attributeFormDefault="unqualified"


xmlns:EPIA="http://www.egemin.com/Epia/StringResources">

And now you can apply uniqueness constraint on StrRes element:

<xs:complexType name="StrResourcesType">
<xs:sequence>
<xs:element name="StrRes" type="StrResType" minOccurs="0"
maxOccurs="unbounded">
<xs:unique name="LocalazedUnique">
<xs:selector xpath="EPIA:Localized"/>
<xs:field xpath="@EPIA:LanguageAttr"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2

P: n/a
Tanks for the information.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Dirk Declercq wrote:
Is it possible in Xml to enfore the uniqueness of an element based on his attribute value.


Sure. Make sure you've read XML Schema Primer on the topic:
http://www.w3.org/TR/xmlschema-0/#specifyingUniqueness

This schema should be a bit modified to be able to express uniqueness
constraints. Uniqueness in schema relies on XPath patterns and XPath
doesn't support default namespace, which is your schema is based on. So
additionally declare prefix for your namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.egemin.com/Epia/StringResources"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.egemin.com/Epia/StringResources"
elementFormDefault="qualified" attributeFormDefault="unqualified"


xmlns:EPIA="http://www.egemin.com/Epia/StringResources">

And now you can apply uniqueness constraint on StrRes element:

<xs:complexType name="StrResourcesType">
<xs:sequence>
<xs:element name="StrRes" type="StrResType" minOccurs="0"
maxOccurs="unbounded">
<xs:unique name="LocalazedUnique">
<xs:selector xpath="EPIA:Localized"/>
<xs:field xpath="@EPIA:LanguageAttr"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.