Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 15th, 2006, 02:39 PM
Authorized User
 
Join Date: Jan 2006
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
 
Old March 15th, 2006, 02:53 PM
Authorized User
 
Join Date: Jan 2006
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>





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





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