Difficult to tell what your problem is… you tried to post XML, but, of course, the < and > tags disappear.
I suspect you’ve got a namespace issue: the xpath expression /html/head/title doesn’t work if html, head and title are in a namespace.
You need to define a namepspace and modify your xpath expression accordingly. It is a pain, but that’s the name of the game.
Dick Adamssays:
Glad to hear some honest commnets on the state of XML. I’ve been having a devil of a time trying to use Java 5.0’s new XPath functionality with XHTML. For example, javax.xml.xpath handles the following sample XML document just fine:
However, when you add the extra data to make it an XHTML document, like this (taken from http://www.w3.org/TR/xhtml11/conformance.html), you can no longer get even the title from the document:
Difficult to tell what your problem is… you tried to post XML, but, of course, the < and > tags disappear.
I suspect you’ve got a namespace issue: the xpath expression /html/head/title doesn’t work if html, head and title are in a namespace.
You need to define a namepspace and modify your xpath expression accordingly. It is a pain, but that’s the name of the game.
Glad to hear some honest commnets on the state of XML. I’ve been having a devil of a time trying to use Java 5.0’s new XPath functionality with XHTML. For example, javax.xml.xpath handles the following sample XML document just fine:
Virtual Library
Moved to vlib.org.
However, when you add the extra data to make it an XHTML document, like this (taken from http://www.w3.org/TR/xhtml11/conformance.html), you can no longer get even the title from the document:
Virtual Library
Moved to vlib.org.
It doesn’t throw an exception, it just reurns null objects or empty strings. Here’s the code I ran against both documents:
XPathFactory factory=XPathFactory.newInstance();
XPath xPath = factory.newXPath();
File xmlDocument = new File(“c:\tmp\test.htm”);
InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
XPathExpression expression = xPath.compile(“/html/head/title”);
String title = expression.evaluate(inputSource);
System.out.println(title);
Have you encountered this problem with XHTML?