469,900 Members | 1,583 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,900 developers. It's quick & easy.

Uniqueness based on Attribute-value

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
2 1979
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
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.

Similar topics

1 post views Thread by Puvendran Selvaratnam | last post: by
reply views Thread by Don Bate | last post: by
reply views Thread by Olaf Meyer | last post: by
1 post views Thread by Robert Ludewig | last post: by
1 post views Thread by Robert Ludewig | last post: by
1 post views Thread by Mr. Almenares | last post: by
5 posts views Thread by Alan Little | last post: by
8 posts views Thread by gnomff | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.