I have an element named "vendingMachine". This may appear in one of two
places
<customerSite>
<vendingMachines>
or
<plannedVisit>
<installationTask>
Because of this I made an element named vendingMachine and simply referred
to it when needed <xs:element ref="vendingMachine"/>. Various other parts
of my XML need to refer to a vending machine by its serial number. My
questions are
1) How do I define a key on vendingMachine @serialNumber so that it is
unique within the whole document no matter where it appears
2) How do I define a reference to a vending machine which could be in either
place?
Within <xs:schema> I have the following
<xs:key name="vendingMachineSerialNumberKey">
<xs:selector xpath="vendingMachine"/>
<xs:field xpath="@serialNumber"/>
</xs:key>
and using the key:
<xs:keyref name="visitActionVendingMachineSerialNumber"
refer="vendingMachineSerialNumberKey">
<xs:selector
xpath="roundData/visitDay/plannedVisits/plannedVisit/tasks/*"/>
<xs:field xpath="@vendingMachineSerialNumber"/>
</xs:keyref>
but when I validate my XML in XmlSpy I get the following error:
Identity constraint visitActionVendingMachineSerialNumber did not match
within the scope of element <data>. The referenced identity constraint
"vendingMachineSerialNumberKey" is out of scope
Can anyone help?
Thanks!
Pete
1) How do I define a key on vendingMachine @serialNumber so that it is
unique within the whole document no matter where it appears
Place the key definition vendingMachineSerialNumberKey on the root element
of the document. Note the .// in the selector xpath to select all descendant
vendingMachine nodes
<xs:key name="vendingMachineSerialNumberKey">
<xs:selector xpath=".//vendingMachine"/>
<xs:field xpath="@serialNumber"/>
</xs:key>
2. The spec requires that the keyref be defined on an element that is an
ancestor or self of the element on which the key is defined. If you move the
keyref definition to the root element as well, this should work as both the
key and the keyref will be defined on the same element.
Thanks,
Priya
>I have an element named "vendingMachine". This may appear in one of two
>places
[quoted text clipped - 41 lines]
>
> Pete
Stan Kitsis [MSFT] - 06 Jul 2006 20:00 GMT
You will also have to use apporpriate namespace prefixes with xpath
expressions. Assuming that namespace prefix for vendingMachine element is
myTNS, Priya's example should be
<xs:key name="vendingMachineSerialNumberKey">
<xs:selector xpath=".//myTNS:vendingMachine"/>
<xs:field xpath="@serialNumber"/>
</xs:key>

Signature
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
> 1) How do I define a key on vendingMachine @serialNumber so that it is
> unique within the whole document no matter where it appears
[quoted text clipped - 59 lines]
>>
>> Pete