The problem your'e having is due to the fact that XPath doesn't recognize the default namespace when resolving name steps. To fix this, you must specify a prefix in your xpath, such as:

/html:html/html:head/html:title

How you set the 'html' prefix is dependent upon how you're generating your XPath selectors. I assume you're using DOM4J since you posted to this list, so here's one way that may work:

Document doc = ...;
XPath xpath = doc.createXPath("/html:html/html:head/html:title");
// manually tell the xpath about our prefix
xpath.setNamespaceContext(Collections.singletonMap("html", "http://www.w3.org/1999/xhtml";);
Element titleElem = (Element)xpath.selectSingleNode(doc);


Also, check the archives of the list for more info on this, since it come up a lot.

best,
christian.

On Jun 22, 2004, at 10:38 AM, [EMAIL PROTECTED] wrote:

Hi!

I use CyberNeko in order to parse HTML pages. I now came across some that does
makes me go nuts since the xpath expression /HTML/HEAD/TITLE returns null (so
does everything except /HTML) despite it is there - really. I even "dumped"
the struture using dom4j's DocumentTreeModel and did so manually, too.


It seems that it does have something to do with the xmlns declaration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml";>
  <head>
    <title>...

Well, but what does this have to do with my xpath?

Anybody having a clue about this?


------------------------------------------------------- This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user




-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to