W3C home > Mailing lists > Public > www-ql@w3.org > July to September 2003

Re: Comparing relationships XML vs. RDBs

From: Brian Maso <brian@blumenfeld-maso.com>
Date: Wed, 10 Sep 2003 14:23:11 -0700
Message-Id: <>
To: www-ql@w3.org

(Responding to myself)


 > Use XML Schemas, there is no standard way to represent a relationship 
between an element located
 > in document A to an element in document B. The only intra-element 
relationships are within the
 > same document. Specifically, the usable relationships are the XPath axes 
(child::, parent::,
 > attribute::, etc.) So intra-entity relationships in XML Schemas seem a 
bit weaker to me than in
 > relational schemas.

Re-reading "Essential XML Quick Reference" (Skonnard, Gudgin) chapter on 
XML Schema I realize using <xs:key> and <xs:keyref> elements that XML 
Schema supports intra-element references within the same document. However 
you can't reference between documents, and XPath doesn't have any support 
for these keyref-based references.

The following schema describes a relationship between top-level order 
elements and item elements, expressed in individual order-item elements 
that are children of the order elements.

<xs:complexType name="itemType">
       <xs:element name="name" type="xs:string"/>
       ... <!-- description, supplier, etc. --> ...
     <xs:attribute name="id" type="ID" use="required"/>

<xs:complexType name="orderType">
       ... <!-- order number, salesperson, etc. --> ...
       <xs:element name="order-item" type="tns:orderItemType"
           minOccurs="0" maxOccurs="1"/>

<xs:complexType name="orderItemType">
       <xs:elemenmt name="quantity" type="xs:nonNegativeInteger"/>
       ... <!-- color or other options, extra notes, etc. --> ...
     <xs:attribute name="itemID" type="ID"/>

<xs:element name="items-and-orders">
       <xs:element name="item" type="itemType"
               minOccurs="0" maxOccurs="unbounded"/>
       <xs:element name="order" type="orderType"
               minOccurs="0" maxOccurs="unbounded"/>

   <xs:key name="keyItemID">
     <xs:selector xpath="item"/>
     <xs:field xpath="@id"/>

   <xs:keyref name="OrderItem_to_Item" refer="keyItemID>
     <xs:selector xpath="order/order-item"/>
     <xs:field xpath="@itemID">

XQuery main module listing each item name along with the number of items 
ordered (off the cuff, probably OK -- enough to get the gist):

for $item in (/items-and-orders/item)
let $itemID = $item/@id/value(), $itemName=$item/name/value()
    $itemName, ": ",
    fn:sum(/items-and-orders/order/order-item[@itemID = 

The relationship between order-item and the item it refers to is buried in 
the "[@itemID = $itemID]" XPath predicate. (No way to simply use the 
"OrderItem_to_Item" keyref defined in the schema.)

Using nested FLOWR expressions I guess I could do the same thing like so:

for $item in (/items-and-orders/item)
for $order-item in (/items-and-orders/order/order-item)
where ($item/@id/value() = $order-item/@itemID/value())
    $item/name/value(), ": ",

This "feels" a lot more SQL-y. But its a completely synthetic example: I 
wouldn't keep items and orders all in the same document. What would be a 
"good" way to define my XML DB? Create a collection of "item" documents, 
one item per document? Create a single "items" document holding all "item" 
elements? Create a collection of "order" documents? Or a single "orders" 
document with multiple "order" elements under it? In any of these cases I 
wouldn't be able to define the key/keyref combo that defines the constraint 
on order-item/@itemID values.

Any thoughts on how to define the key/keyref with items and orders stored 
in separate documents?

Brian Maso
Received on Thursday, 11 September 2003 10:10:36 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 22:43:43 UTC