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 12th, 2011, 06:29 AM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Setting unique element restriction

Here is my schema, what I'm unable to do is set unique restrictions on the title element.

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="departments">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="department" minOccurs="1" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="department">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="title" minOccurs="1" maxOccurs="1"/>
				<xs:element ref="subdepartment" minOccurs="1" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="title">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="subdepartment">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
</xs:schema>
What I'm trying is this within the title element tag, XMLSpy accepts it as valid but the restriction does not work if I test duplicate titles in my XML.

Code:
<xs:unique name="testUnique">
	<xs:selector xpath="department"/>
	<xs:field xpath="title"/>
</xs:unique>
Please help!
 
Old February 12th, 2011, 06:40 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Put your xs: unique into the definition of the "departments" element:
Code:
<xs:element name="departments">
  <xs:complexType>...</xs:complexType>
  <xs:unique ...>...</xs:unique>
</xs:element>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 12th, 2011, 02:14 PM
Registered User
 
Join Date: Feb 2011
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thanks this works for "title" as a single element under "department". Another element under "department" is "subdepartment" and this is repeated 5 times for different sub departments.

If I try to enforce these to me unique, XMLSpy returns an error stating:

- Field '{anonymous}' of identity constraint 'UniqueSubDept' evaluates to a node-set with more than one member.

- Error location: departments / department / subdepartment

Here is the code:

Code:
<xs:unique name="UniqueSubDept">
	<xs:selector xpath="department"/>
             <xs:field xpath="subdepartment"/>
</xs:unique>
Please can you let me know how to enforce unique values within repeating elements?

Thanks
 
Old February 12th, 2011, 03:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The general formula is like this: suppose you want every X within a Y to have a unique value for Z (for example, every employee within a company to have a unique works number, or every figure within a chapter to have a unique caption). Then the xs:unique declaration goes in the Y element; the xs:selector is path expression that selects all the X elements starting from the Y, and the xs:field is a path expression that selects the Z element or attribute (and there must only be one) starting from the X.

Now, I don't think I have fully understood your data or your requirement, but it looks to me something like

Code:
<xs:element name="departments">
  <xs:unique ...>
     <xs:selector xpath="department | department//subdepartment"/>
      <xs:field xpath="title"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to determine if an element value is unique? mphare XSLT 2 November 17th, 2008 12:12 PM
Restriction to certain applications PankajGarg10 General .NET 0 April 13th, 2007 01:08 AM
Schema Complex element with restriction problem [email protected] XML 1 June 27th, 2006 11:45 AM
Dynamically Setting X, Y Coordiantes of Element [email protected] Javascript 4 August 21st, 2005 05:01 AM
Search restriction Snib Forum and Wrox.com Feedback 0 August 13th, 2004 11:02 AM





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