Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / July 2006

Tip: Looking for answers? Try searching our database.

Keys and references

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Morris [Droopy eyes software] - 06 Jul 2006 12:52 GMT
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
Priya Lakshminarayanan - 06 Jul 2006 18:20 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.