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 February 28th, 2006, 07:14 PM
Authorized User
 
Join Date: Jan 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two unique Attribues

I would appreciate help on this one.

How do I create a type which has two unique attributes. I know how to make one of the attributes unique but don't know for two. The two attributes have to be independently unique i.e. each one of them must be unique within the context of the current node. It follows that the combination of the two will be unique. Take the element node below for example:

Code:
<computer>
   <part name="chasis" number="234">
      <description>DELL chasis</description>
      <connected>false</connected>
   </part>
   <part name="chasis" number="233">
      <description>DELL chasis</description>
      <connected>false</connected>
   </part>
</computer>
I have created the schema part of this as follows so that the "number" attribute is unique. I want to do the same for the "chasis" attribute but I get errors if I repeat what I did for the "chasis". I can see why I get errors as it is not valid XML because I end up having two element nodes which are the same

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="computer">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="part" type="partType" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>


      <xs:unique name="part">
         <xs:selector xpath="part"/>
         <xs:field xpath="@number"/>
      </xs:unique>





   </xs:element>

   <xs:complexType name="partType">
      <xs:sequence>
         <xs:element name="description" type="xs:string"/>
         <xs:element name="connected" type="xs:boolean"/>
      </xs:sequence>

      <xs:attribute name="name" type="xs:token" use="required"/>
      <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
   </xs:complexType>
</xs:schema>

Note that I am not looking for this.

Code:

      <xs:unique name="part">
         <xs:selector xpath="part"/>
         <xs:field xpath="@number"/>
         <xs:field xpath="@name"/>
      </xs:unique>
Thanks in advance
__________________
Kind Regards,
Shumba
 
Old February 28th, 2006, 07:42 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Perhaps you made the same mistake in your schema as you made in your post: calling the attribute "chasis" when its name is actually "name".

At any rate, if you can't see what the error is when you've got the faulty schema and the error message in front of you, then I certainly can't see what it is without that information.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 28th, 2006, 08:03 PM
Authorized User
 
Join Date: Jan 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response.

Yeah. Thats was just a typo in the post. Should have read 'if I repeat what I did for "chasis"'

I tried doing this


Code:
      <xs:unique name="part">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:unique>
      <xs:unique name="part">
         <xs:selector xpath="part"/>
         <xs:field xpath="@name"/>
      </xs:unique>
I try to validate the XML with the schema and get the error because I have the same element node in the schema.

error:


Description: E sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of ',part'.
URL: http://www.w3.org/TR/xmlschema-1/#sch-props-correct



I am assumming there is a way to ensure two(or more) unique attributes on an element
 
Old February 28th, 2006, 08:37 PM
Authorized User
 
Join Date: Jan 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok. Got it. Just need to use different names like so

Code:
      <xs:unique name="uniqueReference">
         <xs:selector xpath="part"/>
         <xs:field xpath="@ref"/>
      </xs:unique>
      <xs:unique name="uniqueName">
         <xs:selector xpath="part"/>
         <xs:field xpath="@name"/>
      </xs:unique>
   </xs:element>
I had noe realised the names used for "uniqueness" could be any valid xml name.


Thanks for helping
 
Old February 28th, 2006, 08:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The error message means what it says, you can't have two unique constraints with the same name. Just call one of them

<unique name="part1">

and the other

<unique name="part2">

And next time you ask a question, include the error message and the code it was complaining about please!

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 28th, 2006, 09:05 PM
Authorized User
 
Join Date: Jan 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot. It was my first post ever so I appreciate all the advice given.

Kind Regards.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Is uniqueidentifier is always unique ? vinod_yadav1919 SQL Server 2000 4 May 30th, 2008 11:31 AM
Unique Records arholly Access 9 December 14th, 2006 08:22 AM
duplicates and unique sheba0907 SQL Language 6 July 3rd, 2006 02:43 PM
Unique Problem rekha_jsr Classic ASP Basics 0 December 10th, 2005 12:21 AM
Doing addition on Unique Id's [email protected] XSLT 1 August 1st, 2005 02:45 PM





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