One of the fundamental constructs for XSL transformations and XML links, XPath is nonetheless one of the lesser lights of the XML universe. However, if you're serious about developing your XML skills, you need to know it inside out - and this tutorial has all you need to get started.
<step>Cut chicken into cubes, wash and apply lime juice and
<step>Add ginger, garlic, chili, coriander and lime juice
in a separate
<step>Mix well, and add chicken to marinate
for 3-4 hours</step>
<step>Place chicken pieces on skewers and barbeque</step>
apply butter, and barbeque again until meat is tender</step>
with lemon and chopped onions</step>
Now, if I wanted to get to the third ingredient, I would use the path
whish references the text string
Note the predicate used to get the third item in the list.
If I needed to get the number of servings, I could use
The // shortcut will select elements of that name anywhere below the current context node, and is equivalent to the "descendant-or-self" axis. So the path
would select all the "item" nodes in the document, while the path
would reference the "quantity" element under the seventh "item" element and the path
would reference the text node
If I wanted to get really funky, I could use a different predicate to identify the appropriate node - the paths
More examples of this nature are available in the XPath specification at http://www.w3.org/TR/xpath.html
This article copyright Melonfire 2001. All rights reserved.