How do you best express references to keys/ID's in an XSD ?

  • Thread starter Sébastien de Mapias
  • Start date
S

Sébastien de Mapias

Good day,
To people well acquainted with XML schemas manipulation here,
how do you express relationships/references between elements ?

Take the following excerpt from one of the analysts around me:

~ <xs:attributeGroup name="traveller-id-ag">
~ <xs:attribute name="traveller-id" type="string20" use="required"/>
~ </xs:attributeGroup>
~
~ <xs:attributeGroup name="ticket-id-ag">
~ <xs:attribute name="ticket-id" type="string20" use="required"/>
~ </xs:attributeGroup>
~
~ <xs:attributeGroup name="segment-id-ag">
~ <xs:attributeGroup ref="product-id-ag"/>
~ <xs:attributeGroup ref="traveller-id-ag"/>
~ </xs:attributeGroup>
~
~ <xs:complexType name="traveller">
~ <xs:attributeGroup ref="traveller-id-ag"/>
~ <xs:attributeGroup ref="modification-info-ag"/>
~ <xs:sequence>
~ <xs:element name="traveller-info" type="traveller-info"/>
~ <xs:element name="remark" type="remark" minOccurs=... />
~ <xs:element name="message" type="message" minOccurs=... />
~ </xs:sequence>
~ </xs:complexType>
~
~ <xs:complexType name="segment">
~ <xs:attributeGroup ref="segment-id-ag"/>
~ <xs:sequence>
~ <xs:element name="segment-info" type="segment-info"/>
~ <xs:element name="price" type="price" minOccurs="0"/>
~ <xs:element name="ticket-ref" minOccurs="0">
~ <xs:complexType>
~ <xs:attributeGroup ref="ticket-id-ag"/>
~ </xs:complexType>
~ </xs:element>
~ <xs:element name="duration" type="duration" minOccurs=... />
~ <xs:element name="length" type="length" minOccurs=... />
~ </xs:sequence>
~ </xs:complexType>
~
~ <xs:complexType name="ticket">
~ <xs:attributeGroup ref="ticket-id-ag"/>
~ <xs:sequence>
~ <xs:element name="price" type="price" minOccurs="0">
~ </xs:element>
~ [...]
~ </xs:sequence>
~ </xs:complexType>

You see the guy has first declared attribute groups to specify
ID's (simple or composite), that he later uses inside complex
types as real ID's/keys or as references either (which kind of
annoys me): in the complex type "ticket", the "ticket-id-ag"
is a *key*, a string that identifies uniquely a ticket; but
in "segment", it's a reference to the ticket defined below
(but to express the "ref" difference we have, he's put this
"ticket-id-ag" inside an element named "ticket-ref")...

Shouldn't it be more clear-cut and legible to do something
like (and get rid of these attribute groups as well maybe ?):
~ <xs:complexType name="segment">
~ <xs:key name="segment-id"/>
~ <xs:sequence>
~ <xs:element name="segment-info" type="segment-info"/>
~ <xs:element name="price" type="price" minOccurs="0"/>
~ <xs:element name="ticket-ref" minOccurs="0">
~ <xs:keyref name="ticket-id-ref" ref="ticket-id"/>
~ </xs:element>
~ </xs:sequence>
~ </xs:complexType>
~
~ <xs:complexType name="ticket">
~ <xs:key name="ticket-id"/>
~ <xs:sequence>
~ <xs:element name="price" type="price" minOccurs="0">
~ </xs:element>
~ [...]
~ </xs:sequence>
~ </xs:complexType>

or something similar (I'm not sure at all of the syntax
here, perhaps "segment" could be further simplified with:
~ <xs:keyref name="ticket-ref" ref="ticket-id" >
~ </xs:keyref>
with no xs:element surrounding it ?
I have no idea - sorry I'm not very familiar with XML
schemas...) ?

A detail that might have its importance: this comes from
XSD's used with JAXB to generate classes of our protocol...

Thanks a lot in advance for any help and advice about all
this stuff !
Regards,
Sébastien Rigaud
 
M

Martin Honnen

Sébastien de Mapias said:
Shouldn't it be more clear-cut and legible to do something
like (and get rid of these attribute groups as well maybe ?):
~ <xs:complexType name="segment">
~ <xs:key name="segment-id"/>
~ <xs:sequence>
~ <xs:element name="segment-info" type="segment-info"/>
~ <xs:element name="price" type="price" minOccurs="0"/>
~ <xs:element name="ticket-ref" minOccurs="0">
~ <xs:keyref name="ticket-id-ref" ref="ticket-id"/>
~ </xs:element>
~ </xs:sequence>
~ </xs:complexType>
~
~ <xs:complexType name="ticket">
~ <xs:key name="ticket-id"/>
~ <xs:sequence>
~ <xs:element name="price" type="price" minOccurs="0">
~ </xs:element>
~ [...]
~ </xs:sequence>
~ </xs:complexType>

You need four definitions, one for the element or attribute which is an
id or key, one for the element or attribute which is an id reference or
key reference, one for defining the key with xs:key, and the last for
defining the key reference with xs:keyref. See
http://www.w3.org/TR/xmlschema-0/#specifyingKeysAndtheirRefs
 
S

Sébastien de Mapias

See http://www.w3.org/TR/xmlschema-0/#specifyingKeysAndtheirRefs

Thanks for your answer. I'm afraid I still don't understand how key/
references
have to be expressed. In the report.xsd example schema,
1/ I see they use this <selector xpath...> tag: DO YOU HAVE TO USE IT
in combination with KEY/KEYREF's tags ?
2/ this way do you easily see the references of 'regions' and 'parts'
to
purchaseReport ?

Thanks.
SR
 
M

Martin Honnen

Sébastien de Mapias said:
Thanks for your answer. I'm afraid I still don't understand how key/
references
have to be expressed. In the report.xsd example schema,
1/ I see they use this <selector xpath...> tag: DO YOU HAVE TO USE IT
in combination with KEY/KEYREF's tags ?

Yes, you use selector and field as child elements of key and keyref
elements to precisely define what constitutes the key respectively the
key reference.
2/ this way do you easily see the references of 'regions' and 'parts'
to
purchaseReport ?

In that example the 'number' attribute of 'parts/part' elements inside
the 'purchaseReport' element is a key referenced by the 'number'
attribute of 'regions/zip/part' elements.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top