File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Trouble selecting nodes between two siblings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Trouble selecting nodes between two siblings" Watch "Trouble selecting nodes between two siblings" New topic
Author

Trouble selecting nodes between two siblings

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Jul 02, 2008
Posts: 436
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Jul 02, 2008
Posts: 436
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

Okay. So simplify: do you get anything from this XPath expression?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Jul 02, 2008
Posts: 436
Ah, you're right. I misunderstood following and preceding. Problem solved. Thanks, -
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trouble selecting nodes between two siblings
 
Similar Threads
Can you find the bug in <div> I do NOT get sample xml data[bookrss.xml]... RSS Client/Server
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException: 8
how to show user objects in jsp page
I know what to do but don't know how to implement it..
need help for critical path analysis in java