By combining the axis and node test into a location step, and combining multiple location steps into a location path, it becomes possible to locate specific nodes with the document tree quite easily. Using the following XML sample, let's consider some examples.
The path
references the text node
In order to make this a little easier to read (and write), XPath assumes a default axis of "child" if none is specified - which means that I could also write the above path as
Similarly, the path
references the comment string
while the path
references the string
The * character matches all child elements of the context node, while the @ prefix indicates that attributes, rather than elements, are to be matched. The path
would match all the children of the "movie" element, while the path
would refer to all the attributes of the movie element. In case I need a specific attribute - say, "genre", I could use the path
or the path
both of which would reference the value
Finally, the path
would reference the first element under the document root, which also happens to be the outermost element, while the path
selects all the elements in the document. This article copyright Melonfire 2001. All rights reserved.
blog comments powered by Disqus |