More Key/Keyref Questions

Hello all,

Every time I think I've got the XML schema keys and keyrefs, they
baffle me again.  A simplified scenario:

Categories have elements, each of which must have a unique ID. 
Activities reference these elements by ID, and an activity cannot have a
nonexistent element ID.

On the previous advice of  George (thanks much!), my simplified schema
looks something like this:

<xs:element name="prodCategory" type="prodCategory">
	<xs:key name="prodCategory.PK">
		<xs:selector xpath="."/>
		<xs:field xpath="catName"/>
	</xs:key>
	<xs:key name="CatElem.PK">
		<xs:selector xpath="prodElement"/>
		<xs:field xpath="elemID"/>
	</xs:key>
</xs:element>
<xs:complexType name="prodCategory">
	<xs:sequence>
		<xs:element name="catName" type="xs:string"/>
		<xs:element ref="prodElement" maxOccurs="unbounded"/>
	</xs:sequence>
</xs:complexType>

My udnerstanding, through George and plowing through the XML schema
spec, is that the above keys define uniqueness _within a category_ - in
other words, Category1 could not contain two elements with ID #1.

On to elements:

<xs:element name="prodElement" type="prodElement">
	<xs:key name="prodElement.PK">
		<xs:selector xpath="."/>
		<xs:field xpath="elemID"/>
	</xs:key>
</xs:element>
<xs:complexType name="prodElement">
	<xs:sequence>
		<xs:element name="elemID" type="xs:int"/>
		<xs:element name="elemName" type="xs:string"/>
		<xs:element name="elemOrder" type="xs:int"/>
		<xs:element name="elemQuantity" type="xs:int"
default="1"/>
	</xs:sequence>
</xs:complexType>

Do I need prodElement.PK as well to define uniqueness and referral
_across_ categories?  (In which case, perhaps I don't need CatElem.PK as
it defines essentially the same thing.)  Because my activity definition
needs to refer to unique elements in any category:

<xs:element name="activity" type="activity">
	<xs:key name="activityID.PK">
		<xs:selector xpath="."/>
		<xs:field xpath="activityID"/>
	</xs:key>
	<xs:keyref name="elemRef.FK" refer="prodElement.PK">
		<xs:selector xpath="."/>
		<xs:field xpath="elemRef"/>
	</xs:keyref>
</xs:element>
<xs:complexType name="activity">
	<xs:sequence>
		<xs:element name="activityID" type="xs:int"/>
		<xs:element name="elemRef" type="xs:int"
maxOccurs="unbounded"/>
	</xs:sequence>
</xs:complexType>

What I'm trying to do above is make sure that an activity can have any
number of elements, but they must be uniue ID's that exist somewhere in
some category.  I wish I were a little less dense at this - please be
patient with an XML newbie.

Jim Stanley
Media Services, Inc.

Received on Thursday, 29 July 2004 19:13:49 UTC