Jump to content

XPath operator for range selection


kgw

Recommended Posts

Hi, Is there any XPath operator to return a range of elements? For example, /bookstore/book[2] will return 2nd book element.How can I return 2nd to 4th book element? I tried /bookstore/book[2 to 4] in Altova XMLSpy 2006 to but instead it returns all book elements (/bookstore/book) :)Thanks :)Btw this is the sample xml: (I took from XPath tutorial)

<bookstore><book category="COOKING">  <title lang="en">Everyday Italian</title>  ...</book><book category="CHILDREN">  <title lang="en">Harry Potter</title>  ...</book><book category="WEB">  <title lang="en">XQuery Kick Start</title>  ...</book><book category="WEB">  <title lang="en">Learning XML</title>  ...</book></bookstore>

Link to comment
Share on other sites

I think that by using this kind of predicate, you're actually pointing to the element book, which contains an element called 2, which would be impossible (as far as I know, element names can't begin with a number). Instead I think, you should check the position(), like this:

/bookstore/book[position() = 2 to 4]

I don't know if that would work either though. Just a though.

Link to comment
Share on other sites

I also think that when your XML is:

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING">  <title lang="en">Everyday Italian</title>  <author>Giada De Laurentiis</author>  <year>2005</year>  <price>30.00</price>  <order>1</order></book><book category="CHILDREN">  <title lang="en">Harry Potter</title>  <author>J K. Rowling</author>  <year>2005</year>  <price>29.99</price>  <order>2</order></book><book category="WEB">  <title lang="en">XQuery Kick Start</title>  <author>James McGovern</author>  <author>Per Bothner</author>  <author>Kurt Cagle</author>  <author>James Linn</author>  <author>Vaidyanathan Nagarajan</author>  <year>2003</year>  <price>49.99</price>  <order>3</order></book><book category="WEB">  <title lang="en">Learning XML</title>  <author>Erik T. Ray</author>  <year>2003</year>  <price>39.95</price>  <order>4</order></book></bookstore>

, you could select the "XQuery Kick Start" and "Harry Potter" books by using:

set nodes=xmlDoc.selectNodes("/bookstore/book[order > 1 && order < 4]/title")

I am pointing at the order element, I think that it should work too this way.

Link to comment
Share on other sites

Hi Aleksanteri,

set nodes=xmlDoc.selectNodes("/bookstore/book[order > 1 && order < 4]/title")

I've tried to run in Altova XMLSpy and Tryit Editor but it doesn't work. I changed the "&&" to "and" but it also doesnt work.
Link to comment
Share on other sites

Excuse my ignorance, but... who cares?! "position()>=2 and position()<=4" works, right?

Link to comment
Share on other sites

Well you were right to change the && to an and as I don't think you can use the &&. Try this:

/bookstore/book[child::order > 1 and child::order < 4]/title

*shrugs* just try all the permutaions you can think of. If that doesn't work take off the /title and then take off one of the conditions.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...