I’m trying to design an XML document structure for my application.
I want to store a list of holes like following
<Holes> <Hole id='1' dia='0.1' depth='2'/> <Hole id='2' dia='0.2' depth='1.67'/> <Hole id='3' dia='0.3' depth='0.44'/> </Holes>
In another part of my document I want to refer to a hole by its id. e.g.
<Drill useHoleWithId='1'/>
When my code finds above <Drill> element I want it to retrieve the values of ‘dia’ and ‘depth’ attributes in the <Hole> element that has id=’1′.
Of course I can search for a <Hole> element with id equal to the value of ‘useHoleWithId’ and then get the values of the attributes, but I thought maybe there’s a better way to do this using some XML trick. Is there?
PS – Though I don’t have any idea about them, may be any of XPath, XLink, XQuery or XPointer could help.
XPath is certainly one way to do it. An Xpath query to find the hold with id 1 would be something like
Holes/Hole[@id='1']