If you are not interested in the namespace qualification you could just look
for elements with a local name of "div" using an Xpath expression like this:

//.[local-name()='div'[EMAIL PROTECTED]'content_left']



-----Original Message-----
From: Ben Munat [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 24, 2006 2:58 PM
To: dom4j-user@lists.sourceforge.net
Subject: [dom4j-user] xpath annoyances


I'm having trouble with xpath's returning valid results in dom4j. My
co-worker was trying to select a div in an xhtml 
file based on a class attributes and was getting any results, even though he
could get results with the same XPath in 
jedit (using saxon) and it worked fine.

I told him about the namespace problem and added the code -- as Edwin has
suggested in the past -- to add a map of 
namespaces, with just "" mapped to "http://www.w3.org/1999/xhtml";. This
works for selecting unqualified elements; i.e. 
without the [EMAIL PROTECTED]'foo'] predicate. (Well, we struggled for a while 
with
it working on my machine but not on his and 
discovered that he had jaxen-1.1-beta-6 and I had beta-4... so beta-6
apparently introduced a bug!). However, I have 
been trying for hours to get "//[EMAIL PROTECTED]'content_left']" to work. I'll
paste the code/html below.

Really hoping someone's listening and can help here... Edwin, my savior, you
out there?

b

code:

     SAXReader reader = new SAXReader();
     Document document = reader.read(new File("findings.html"));
     XPath xp =
DocumentFactory.getInstance().createXPath("//[EMAIL PROTECTED]'content_left']");
     Map ns = new HashMap();
     ns.put("","http://www.w3.org/1999/xhtml";);
     xp.setNamespaceURIs(ns);
     List nodes = xp.selectNodes(document.getRootElement());


html:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>

<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
     <title>Findings</title>
</head>
<body>
<div id="container">
        <div class="content">
                <div class="label">Decision</div>
                <div id="Decision" class="text">The Decision...</div>
        </div>

        <div class="content_right">
                <div class="content_right_inner">
                        <div class="label">Status</div>
                        <div id="Status" class="text">Pending</div>
                </div>
        </div>

        <div class="content_header"></div>

        <div class="content_left">
                <div class="label">Primary</div>
                <div id="Primary" class="text">Foo Bar</div>
                <div class="line">&nbsp;</div>

                <div class="label">Number</div>
                <div id="Number" class="text">9876543210</div>

                <div class="label">Run Date/Time</div>
                <div id="RunDate" class="text">02/20/2006 12:00:00</div>
                <div class="line"></div>
        </div>

        <div class="content_right">
                <div class="content_right_inner">
                        <div class="label">Secondary</div>
                        <div id="Secondary" class="text">Foo Baz</div>
                        <div class="line">&nbsp;</div>
                        <div class="line">&nbsp;</div>

                        <div class="label">Submitted by</div>
                        <div id="SubmittedBy" class="text">Foo Bar</div>
                </div>
        </div>

</div>

</body>
</html>


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to