tomcat-taglibs-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rahul P Akolkar <>
Subject Re: Problem with namespaces jstl / xml
Date Tue, 31 May 2005 19:23:58 GMT
news <> wrote on 05/31/2005 05:52:18 AM:

> I guess this is a namespace-uri issue, after lots of digging around.
> Please could someone explain what my xpath should be to output the 
> version number of the following (atom) xml (either in JSTL or plain old 
> xpath? I just can't get it.
> <feed version="0.3" xml:lang="en-US" xmlns="">
> </feed>

Assuming you have a document whose namespace URI(s) you do not know (or 
worse, keep changing), it is common to explicitly bind these to namespace 
prefixes so that the expanded names are in accordance with the XPath "data 
model". In your case, you can try this (untested):

// Say 'document' holds the Document object
Element root = (Element) document.getDocumentElement();
String my_ns = root.getAttribute("xmlns");
root.setAttributeNS("", "xmlns:myns", my_ns);
String xpath_ver = "/myns:feed/@version";
Node ver = XPathAPI.selectSingleNode(root, xpath_ver, root);
// ver.getNodeValue() should give you the version number, as a String

// Likewise, this should get you the <entry> elements:
String xpath_entry = "/myns:feed/myns:entry";
NodeList items = XPathAPI.selectNodeList(root, xpath_entry, root);
// You can now iterate over the <entry> items found

I am not aware of a solution based purely on JSTL's XML taglib.


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message