help with xml schema -- making a unique key

X

X. Lee

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!
 
P

Priscilla Walmsley

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top