| Article: |
Parsing an XML Document with XPath | |
| Subject: | Parsing a Namespace node with JDK 5.0 | |
| Date: | 2005-02-17 07:55:52 | |
| From: | Gerg | |
|
Response to: Parsing a Namespace node with JDK 5.0
|
||
| Thanks for the examples. Could you please also show an example of an xpath that includes two different namespaces prefixes? If a journal's article could be in different languages, you might want to add an "xml:lang" attribute to the "article" element. (Where the "xml" prefix in XML documents implicitly refers to the "http://www.w3.org/XML/1998/namespace" namespace without being explicitly declared as such with an "xmlns" attribute in the root element - as I understand it.) Presumably, the xpath expression to return the value of that attribute would be "/catalog/journal:journal/article/@xml:lang". In such an expression it would seem that we need to xpath.setNamespaceContext(NamespaceContextImpl) twice, with two different NamespaceContextImpl instances, one for each prefix ("journal" and "xml"). For me, though, evaluating this expression returns nothing, even if I use a NamespaceContextImpl class (or two). Hmmmmmm. | ||
Showing messages 1 through 3 of 3.
-
Parsing a Namespace node with JDK 5.0
2005-02-17 08:01:53 Deepak Vohra | [View]
-
Parsing a Namespace node with JDK 5.0
2005-02-18 02:16:18 Gerg [View]
Thanks, that works like a charm! -
Parsing a Namespace node with JDK 5.0
2006-02-22 09:15:26 Gerg [View]
I'm getting an unexplanatory XPathExpressionException when I evaluate the following xpath
/xhtml:html/xhtml:body//xhtml:div[@class='reviewlist']
after setting a namespace context
NamespaceContextImpl nsctx = new NamespaceContextImpl();
nsctx.setNamespaceURI("xml", "http://www.w3.org/XML/1998/namespace");
nsctx.setNamespaceURI("xhtml", "http://www.w3.org/1999/xhtml");
xpath.setNamespaceContext(nsctx);
The source XML document is an XHTML web page with its root element being declared as belonging to http://www.w3.org/1999/xhtml namespace:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
I can't see anything wrong with the xpath expression, nor can I find any documentation explaining why an XPathExpressionException would be thrown in this case.
Could you tell any possisble causes for the exception being thrown?
Thanks.



NamespaceContextinterface with the provision to set more than one prefixes for a uri.