|
|
 |
| XML General XML discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XML section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

October 23rd, 2009, 10:35 AM
|
|
Registered User
|
|
Join Date: Oct 2009
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Linking the attribute value in XML
Hi All,
I have a Xml document and a Xml Schema document. In xml Schema i have define sum type which i am using to validate XMl Document.
I want to link the attribute value with one another.
As per the below code i want Regionid and regionType should be linked with each other.
For example : In XMl document it should only allow RegionID="ER" and reion="Europe and Russia"
it should not allow other values with reionID="ER" and vice versa.
below is the Code of XMl Schema :
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="User" minOccurs="0" maxOccurs="6">
<xs:complexType>
<xs:attribute name="region" use="required" type="regiontype"/>
<xs:attribute name="regionId" use="required" type="regionIdtype"/>
</xs:complexType>
</xs:element>
</xs:schema>
<xs:simpleType name="regiontype">
<xs:restriction base="xs:NMTOKENS">
<xs:enumeration value="Europe and Russia"/>
<xs:enumeration value="North America"/>
<xs:enumeration value="Latin and South America"/>
<xs:enumeration value="Africa"/>
<xs:enumeration value="Middle East"/>
<xs:enumeration value="Far East and Australsia"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="regionIdtype">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="ER"/>
<xs:enumeration value="NA"/>
<xs:enumeration value="LSA"/>
<xs:enumeration value="AF"/>
<xs:enumeration value="ME"/>
<xs:enumeration value="FEA"/>
</xs:restriction>
</xs:simpleType>
XMl Structure :
===============
<?xml version="1.0" encoding="UTF-8"?>
<User regionId="ER" region="Europe and Russia">
</User>
How can i achieve this.
Thanks
-Hari
|

October 23rd, 2009, 10:42 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
You would need to "normalize" your data i.e. you need to put only the regionId attribute on the User element and then you need to define a Regions element with Region child elements where RegionId and RegionName are listed.
Then you can define a key/keyreference.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

October 23rd, 2009, 11:01 AM
|
|
Authorized User
|
|
Join Date: Apr 2008
Location: , , .
Posts: 36
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Thanks for your reply.
Could you please give me some example how to do that.
Thanks again.
|

October 23rd, 2009, 11:52 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
Thinking about it again my suggestion is not without problems as it requires you to duplicate the information specified in the simply types as data in the instance document but as long as no one comes up with something better here is what I had in mind:
Code:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="User" minOccurs="0" maxOccurs="6">
<xs:complexType>
<xs:attribute name="regionId" use="required" type="regionIdtype"/>
</xs:complexType>
</xs:element>
<xs:element name="Regions">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Region">
<xs:complexType>
<xs:attribute name="Id" type="regionIdtype"/>
<xs:attribute name="Name" type="regiontype"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="regid">
<xs:selector xpath="Regions/Region"/>
<xs:field xpath="@Id"/>
</xs:key>
<xs:keyref name="regidref" refer="regid">
<xs:selector xpath="User"/>
<xs:field xpath="@regionId"/>
</xs:keyref>
</xs:element>
<xs:simpleType name="regiontype">
<xs:restriction base="xs:NMTOKENS">
<xs:enumeration value="Europe and Russia"/>
<xs:enumeration value="North America"/>
<xs:enumeration value="Latin and South America"/>
<xs:enumeration value="Africa"/>
<xs:enumeration value="Middle East"/>
<xs:enumeration value="Far East and Australsia"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="regionIdtype">
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="ER"/>
<xs:enumeration value="NA"/>
<xs:enumeration value="LSA"/>
<xs:enumeration value="AF"/>
<xs:enumeration value="ME"/>
<xs:enumeration value="FEA"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Code:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2009102301Xsd.xml">
<User regionId="ER"/>
<Regions>
<Region Id="ER" Name="Europe and Russia"/>
<Region Id="NA" Name="North America"/>
<Region Id="LSA" Name="Latin and South America"/>
<Region Id="AF" Name="Africa"/>
<Region Id="ME" Name="Middle East"/>
<Region Id="FEA" Name="Far East and Australsia"/>
</Regions>
</Root>
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|

October 23rd, 2009, 12:44 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Location: , , .
Posts: 36
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thanks Martin. It is working as i wanted.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |