views:

29

answers:

1

Maybe I'm not seeing the forest for the trees, but here it goes:

I'm "designing" an XML document and have so far come up with something like the following:

<element key="root">
    <data>...</data>
    <elements>
        <element key="foo">
            <data>...</data>
        </element>
        <element key="bar">
            <data>...</data>
        </element>
    </elements>
</element>

So it's a simple hierarchical structure. What I want to do now is have references from one element to any other element anywhere in the hierarchy. That would be trivial if each element had a unique ID, but they don't. So far I only plan on guaranteeing that each element's key is unique within its level (much like file names in a directory structure).

In other words, if I had fully qualified keys such as root.foo, guaranteeing referential integrity would be simple. But then I'd be storing redundant information (I already know that foo is a sub element of root, why store that information twice?).

I realize that this is essentially a cosmetic problem. One of the simplest solutions is probably to just auto-assign IDs and be done with it. But this is fairly inelegant (and error-prone unless you have a nice front end for editing the file), so I was hoping for a nicer way to do it.

Is there a way to implement this in XML Schema?

A: 

Use <xs:key> and <xs:keyref>

Keys are unique within specified context so they don't need to be globally unique like ID:s <xs:key> contains <xs:selector> element that specify the scope or context of the key (key value/s must be unique across this set) and <xs:field> element that defines the key nodes. A key can have multiple fields in which case their combination must be unique. <xs:key> and <xs:keyref> are used within an <xs:element> declaration.

jasso

related questions