Support Requests item #1769459, was opened at 2007-08-07 15:04
Message generated for change (Comment added) made by mskells
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=216035&aid=1769459&group_id=16035

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: None
Group: None
Status: Open
Priority: 5
Private: No
Submitted By: Michael (mthenderson)
Assigned to: Nobody/Anonymous (nobody)
Summary: xpath fails to return results

Initial Comment:
I am having a issue using xpath.  We are receiving a file from an external 
vender.  When we use xpath to parse the file, we don’t get any results. 
Below is a brief example.

<?xml version="1.0" encoding="UTF-8"?>
<TXLife Version="2.16.01" xmlns="http://ACORD.org/Standards/Life/2"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://ACORD.org/Standards/Life/2 TXLife2.16.01.xsd">
        <TXLifeRequest>
                <TransRefGUID>abc1234567</TransRefGUID>
        </TXLifeRequest>
</TXLife>



The following code returns no result 
List nodes = doc.selectNodes("//TransRefGUID");

I realize it's due to the default namespace declaration, but the file comes 
from an external vendor and we have not control over it.

Is there a fix for this?  Is there a work around?  We have no control over the 
incoming xml.

Mike Henderson


----------------------------------------------------------------------

Comment By: Mike Skells (mskells)
Date: 2007-08-07 20:16

Message:
Logged In: YES 
user_id=258940
Originator: NO

Hi Mike,
As you say the issue is the default namespace - the way around this is to
use an explicit namespace in the xpath query. The prefix in the query has
no bearing in the actual query so the following demo show how to do this
--------------
import java.io.StringReader;
import java.util.Collections;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;

public class xp {

    public static void main(String[] args) throws Exception {
        String xml = "" +
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<TXLife Version=\"2.16.01\"
xmlns=\"http://ACORD.org/Standards/Life/2\";
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";
xsi:schemaLocation=\"http://ACORD.org/Standards/Life/2
TXLife2.16.01.xsd\">" +
                "    <TXLifeRequest>" +
                "        <TransRefGUID>abc1234567</TransRefGUID>" +
                "    </TXLifeRequest>" +
                "</TXLife>";
        Document doc = new SAXReader().read(new StringReader(xml));
 
        XPath xpath = new DefaultXPath("//x:TransRefGUID");
        
        xpath.setNamespaceURIs(Collections.singletonMap("x",
"http://ACORD.org/Standards/Life/2";));
        
        List nodes = xpath.selectNodes(doc);
        
        System.out.println(nodes);
    }
}
-------

this produces the following output
[EMAIL PROTECTED] [Element: <TransRefGUID uri:
http://ACORD.org/Standards/Life/2 attributes: []/>]]

Mike Skells


----------------------------------------------------------------------

Comment By: Michael (mthenderson)
Date: 2007-08-07 20:03

Message:
Logged In: YES 
user_id=1862440
Originator: YES

Thanks, that did the trick.

Mike

----------------------------------------------------------------------

Comment By: Nobody/Anonymous (nobody)
Date: 2007-08-07 15:10

Message:
Logged In: NO 

Use DocumentFactory for creating document and set it's namespace map for
XPath:

namespaces.put("default", "http://ACORD.org/Standards/Life/2";);

DocumentFactory documentFactory = DocumentFactory.getInstance();
documentFactory.setXPathNamespaceURIs(namespaces);

reader = new SAXReader(documentFactory);

Than you can use "default" as prefix for your namespace:

List nodes = doc.selectNodes("//default:TransRefGUID");

You can try empty prefix:

namespaces.put("", "http://ACORD.org/Standards/Life/2";);

but I can't remember if this works.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=216035&aid=1769459&group_id=16035

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
dom4j-dev mailing list
dom4j-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-dev

Reply via email to