Key constraint for attribute of different elements (XML schema)

T

tthunder

Hi @all,

I am trying to set a key constraint in my XML Schema, which concers all
elements containing an attribute named "ID"

My XML example:

<root>
<x ID='1'>
<y ID='2'>
</y>
<y ID='3'>
</y>
<z>
</z>
</x>
<x ID='4'>
</x>
</root>

The problem is to set selector and field...
Of course this works for this example:

<xs:key name="UniqueID">
<xs:selector xpath="x|x/y"/>
<xs:field xpath="@ID"/>
</xs:key>

The problem is, that I have many many different tags. So I want to do
something like this:

<xs:key name="UniqueID">
<xs:selector xpath="*[@ID]"/>
<xs:field xpath="@ID"/>
</xs:key>

But this is not XML schema compatible :(

Ref:
http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/structures.html#coss-identity-constraint

Any workarounds, ideas, tipps?

Thanks,
Kirsten
 
P

Priscilla Walmsley

Hi,

What about using a unique constraint instead? That way, if the ID
attribute appears, it must be unique. If it doesn't appear, that's not
a problem. So you could use:

<xs:unique name="UniqueID">
<xs:selector xpath=".//*"/>
<xs:field xpath="@ID"/>
</xs:unique>

Hope that helps,
Priscilla
 
T

tthunder

The problem is, that there is a problem if ID does not appear.
Therefore I tried using the predicate [@ID].

BTW: I'm using Altova XML Spy 2006 for validating...
 
P

Priscilla Walmsley

If you use "unique" instead of "key", there should not be a problem if
the ID attribute does not appear - that is why I suggested changing it.

If XML Spy says it's a problem, there is a bug.

Hope that helps,
Priscilla
 
P

Priscilla Walmsley

Hi,

No, you can have a keyref that refers to a "unique" constraint also.

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,969
Messages
2,570,161
Members
46,709
Latest member
AustinMudi

Latest Threads

Top