tags:

views:

38

answers:

1

Looking at w3schools for xPath (feel free to give me other references), you can select all the children of bookstore, but what if I wanted to only select the second book element? How can I do that?

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>
+3  A: 

You could select the n-th element with [n], e.g.

/bookstore/book[2]

See "Predicates" in http://www.w3schools.com/XPath/xpath_syntax.asp for detail.

KennyTM
Positions are rarely a good choice in XML. Better is some other detail that allows you to select the node you care about.
geoffc
@KennyTM: Off topic: I think you should open a discussion on Meta about your `xsltprocessor` to `xslt` tag synonym proposal. Or at least check chat discussion at http://chat.meta.stackoverflow.com/rooms/165/xslt-and-xpath
Alejandro