Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble selecting nodes between two siblings

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm using Java 6. Let's say I have a document with these nodes ...



I want to write an Xpath expression to select all the nodes (they may not necessarily be "p"'s) between the <h2> nodes with text descriptions "Description" and "Cost", as appear above. How do I do this?

I tried



but get the Java exception, "com.sun.org.apache.xpath.internal.XPathException: Can not convert #BOOLEAN to a NodeList!". - Dave
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that expression is of the form "a and b" and so its value is boolean. But presumably your code tells the XPath object to treat it as a node-list (because that's what you expect it to produce).

The "and" needs to be inside the [brackets] which qualify the nodes you're looking for. Something like this?

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I changed the a little (The first h2 marker has text, "Description" and the second h2 marker has text, "Cost") ...



but I'm not getting any results. I also tried removing the "//" from the beginning of the xpath. The URL I'm running this on is here -- http://www.foresightdesign.org/events/detail.php?id=1871 . What else am I missing from the above expression? - Dave
 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha. You're missing the namespace. Your "h2" in the XPath expression refers to an h2 element which is in no namespace, but the h2 elements in the document you linked to are actually in the XHTML namespace.

So provide a namespace context for your XPath object -- let's say it maps the "http://www.w3.org/1999/xhtml" namespace URI to the prefix "x" -- and then use "x:h2" instead of "h2" for the element names.
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I don't think this is a namespace issue. For example, this expression accurately returns an element ...



Notice the use of "h2" without a preceding namespace. The above returns the "<h2>Description</h2>" element from the page. Here is the Java code I'm using to find the element ...

 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So simplify: do you get anything from this XPath expression?

 
Paul Clapham
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Alvarado wrote:The first h2 marker has text, "Description" and the second h2 marker has text, "Cost"...


Then shouldn't you be looking for nodes which have a preceding h2 sibling with text 'Description' and a following h2 sibling with text 'Cost'?
 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, you're right. I misunderstood following and preceding. Problem solved. Thanks, -
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic