key and keyref

M

Mike

In my XML document, I am defining a key on an element, which works fine. I
also have the corresponding keyref element in place to enforce ref.
integrity.

The issue: it's not necessary that an entity have a parent. But, if a parent
ID is specified I want to validate that against the key. For example:

<Objects>
<Object>
<ID>1</ID>
<ParentID/>
</Object>
<Object>
<ID>2</ID>
<ParentID>1</ParentID>
</Object>
</Objects>

In this example, object ID "2" is a child of "1". Since "1" is the parent,
I don't need to validate ParentID (i.e. when it doesn't have a value, don't
validate with keyref).

Any ways around this?

Thanks.
 
S

Steve Jorgensen

I would make the ParentID element optional rather than making its value
optional. If you do that, you're problem is automatically solved because if
and only if the element exists, its value must match a key.
 
M

Mike

Thanks, Steve!

Steve Jorgensen said:
I would make the ParentID element optional rather than making its value
optional. If you do that, you're problem is automatically solved because
if
and only if the element exists, its value must match a key.
 
J

JAPISoft

Hi Mike,

May be you coud specify that the first object has no parent content with
the nil attribute.

<Objects xmlns:xsi="http://www.w3.org/...">
<Object>
<ID>1</ID>
<ParentID xsi:nil="true"/>
</Object>
...
</Objects>

When you use a nil attribute it will notify to the parser to ignore this
node content and thus you can have only one definition inside your W3C
Schema (simplifying key and keyref usage).

Best wishes,

A.Brillant
EditiX - XML Editor and XSLT Debugger
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top