Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > help with xml schema -- making a unique key

Reply
Thread Tools

help with xml schema -- making a unique key

 
 
X. Lee
Guest
Posts: n/a
 
      07-26-2004
Hi, please take a look at this schema and tell me how I can make the
value of my AttachmentKey element(s) unique within the entire instance
document.

I know that I have to use <key> or <unique>, but I can't get it to
work. I don't have knowledge of xpath and the examples I find on the
web aren't working.

here is my schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="test.ns.abc">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ns:A"/>
<xsd:element ref="ns:Attachment" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Attachment">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ns:AttachmentKey"/>
<xsd:element ref="ns:AttachmentName"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AttachmentText" type="xsd:string"/>
<xsd:element name="AttachmentName" type="xsd:string"/>
<xsd:element name="AttachmentPageTotal" type="xsd:int"/>
<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ns:B"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="B">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ns:Attachment"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AttachmentKey"/>
</xsd:schema>

--------------

As you can see, AttachmentKey is a subelement of Attachment, and is
used in more than one place in the example above. I want it so that
when the instance document is created, no matter how many instances of
Attachment is in it, each AttachmentKey value is unique, so that I can
use them as keys.

So this following instance doc would be invalid:

<root>
<A>
<B>
<Attachment>
<AttachmentKey>1</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
</B>
</A>
<Attachment>
<AttachmentKey>1</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
<Attachment>
<AttachmentKey>2</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
</root>



But this one would be valid:

<root>
<A>
<B>
<Attachment>
<AttachmentKey>1</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
</B>
</A>
<Attachment>
<AttachmentKey>2</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
<Attachment>
<AttachmentKey>3</AttachmentKey>
<AttachmentName>Name</AttachmentName>
</Attachment>
</root>

thanks!
 
Reply With Quote
 
 
 
 
Priscilla Walmsley
Guest
Posts: n/a
 
      07-27-2004
Hi,

Within your declaration of root (before the </xs:element>), you should
add

<xsd:key name="uniqueAttachment">
<xsd:selector xpath=".//ns:Attachment"/>
<xsd:field xpath="ns:AttachmentKey"/>
</xsd:key>

You also need to give AttachmentKey a simple type (or a complex type
with simple content).

Hope that helps,
Priscilla

-----------------------------------------------------
Priscilla Walmsley
Author, Definitive XML Schema (Prentice Hall PTR)
http://www.datypic.com
-----------------------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
Validation with XSD using XML::LibXML::Schema, and XML::Validator::Schema huntingseasonson@gmail.com Perl Misc 5 11-29-2006 12:37 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
XML Schema to XML Schema Conversion Hari Om XML 1 03-02-2004 09:04 PM
XML schema regular expressions question and recommended XML Schema book Fred Smith XML 1 02-05-2004 11:12 AM



Advertisments