Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 15th, 2006, 02:39 PM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference to existing attribute value

I have an instance document in which I need to enforce that an attribute value is unique. Also, some other element of the document relates to this unique attribute value.

Here is an example XML instance document I made up to explain my problem

Code:
<?xml version="1.0" encoding="UTF-8"?>
<parts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="test.xsd">
   <part ref="44" name="processor">
      <description>Standard floppy</description>
      <related-part>4</related-part>
   </part>
   <part ref="4" name="motherboard">
      <description>Standard floppy</description>
      <related-part>55</related-part>
   </part>
</parts>

And here is the XML Schema document

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="parts">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="part" type="partType" minOccurs="2" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
      <!-- Each part must have a unique "ref" attribute and "name" combination 
         Same part for different computer make could have the same name but needs
         a different reference to track the part -->
      <xs:unique name="partRef">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:unique>
      <xs:unique name="partRefAndName">
         <xs:selector xpath="item"/>
         <xs:field xpath="@partNum"/>
         <xs:field xpath="productName"/>
      </xs:unique>
   </xs:element>

   <xs:complexType name="partType">
      <xs:sequence>
         <xs:element name="description" type="xs:string"/>
         <xs:element name="related-part" maxOccurs="unbounded"/>
      </xs:sequence>
      <!-- Update to use attribute group
         Remove whitespaces in names -->
      <xs:attribute name="name" type="xs:token" use="required"/>
      <xs:attribute name="ref" type="xs:positiveInteger" use="required"/>
   </xs:complexType>
</xs:schema>
Now within the parts context, I want to create a key to the @ref attribute and ensure that the value of related-parts refers to a existing @ref

I have done this below but it does not work - no error messages


Code:
      <xs:key name="key">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:key>
      <xs:keyref name="refKey" refer="key">
         <xs:selector xpath="part"/>
         <xs:field xpath="related-parts"/>
      </xs:keyref>
Please, let me know if you need more information

Thanks
__________________
Kind Regards,
Shumba
Reply With Quote
  #2 (permalink)  
Old March 15th, 2006, 02:53 PM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok. Got it but not sure if it is the best way to do it. Please advise at any rate. Thanks

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="parts">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="part" type="partType" minOccurs="2" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>
      <!-- Each part must have a unique "ref" attribute and "name" combination 
         Same part for different computer make could have the same name but needs
         a different reference to track the part -->
      <xs:unique name="partRef">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:unique>
      <xs:unique name="partRefAndName">
         <xs:selector xpath="part"/>
         <xs:field xpath="@partNum"/>
         <xs:field xpath="productName"/>
      </xs:unique>


      <xs:key name="key">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:key>
      <xs:keyref name="refKey" refer="key">
         <xs:selector xpath="part/related-part"/>
         <xs:field xpath="."/>
      </xs:keyref>

   </xs:element>

   <xs:complexType name="partType">
      <xs:sequence>
         <xs:element name="description" type="xs:string"/>
         <xs:element name="related-part" type="xs:positiveInteger" maxOccurs="unbounded"/>
      </xs:sequence>
      <!-- Update to use attribute group
         Remove whitespaces in names -->
      <xs:attribute name="name" type="xs:token" use="required"/>
      <xs:attribute name="ref" type="xs:positiveInteger" use="required"/>
   </xs:complexType>
</xs:schema>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
Object Existing moedev Access 3 April 12th, 2006 02:48 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM
Add an attribute to existing table peter_budo SQL Language 9 April 24th, 2005 01:35 PM



All times are GMT -4. The time now is 11:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.