Support Requests item #1769459, was opened at 2007-08-07 17:04
Message generated for change (Comment added) made by filipjirsak
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: Filip Jirsák (filipjirsak)
Date: 2007-08-08 07:41

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

You should decorate all tag names with namespace prefix:

//x:[EMAIL PROTECTED]//x:Holding[child::x:[EMAIL PROTECTED] =
'3']]/x:Policy/x:Life/x:Coverage/x:LifeParticipant[child::x:[EMAIL 
PROTECTED]'18']]/@PartyID]/x:Person/x:LastName



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

Comment By: Michael (mthenderson)
Date: 2007-08-07 23:24

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

Ok, both of these solutions work for the simpler searches.  
However, a number of the xpath statements I need to build are quite
complicated.  For example to get the owners last name, I would use the
following.

//[EMAIL PROTECTED]//Holding[child::[EMAIL PROTECTED] =
'3']]/Policy/Life/Coverage/LifeParticipant[child::[EMAIL 
PROTECTED]'18']]/@PartyID]/Person/LastName

I tried the following with no result
//x:[EMAIL PROTECTED]//x:Holding[child::[EMAIL PROTECTED] =
'3']]/Policy/Life/Coverage/LifeParticipant[child::[EMAIL 
PROTECTED]'18']]/@PartyID]/Person/LastName

Here is a trimmed version of the XML.  Please note, I may have numerous
holdings and numerous parties.  I need to use the type codes and ids to
relate the two and get the correct name.  

<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">
        <UserAuthRequest>
                <VendorApp>
                        <VendorName VendorCode="86">Finetre</VendorName>
                </VendorApp>
        </UserAuthRequest>
        <TXLifeRequest PrimaryObjectID="Proposed_Holding_1">
                <TransRefGUID>abc1234567</TransRefGUID>
                <TransType tc="103"/>
                <NoResponseOK tc="1">True</NoResponseOK>
                <OLifE>
                        <SourceInfo>
                                <CreationDate>2006-12-07</CreationDate>
                                <CreationTime>15:04:15</CreationTime>
                                <SourceInfoName>Finetre</SourceInfoName>
                        </SourceInfo>
                        <Party id="Owner_Party_1">
                                <PartyTypeCode tc="1">Person</PartyTypeCode>
                                <Person>
                                        <FirstName>Roberta</FirstName>
                                        <LastName>Allen</LastName>
                                </Person>
                        </Party>
                        <Holding id="Proposed_Holding_1">
                                <HoldingTypeCode tc="2">Policy 
(Life)</HoldingTypeCode>
                                <HoldingStatus tc="3">Proposed</HoldingStatus>
                                <Purpose tc="21">Family Income</Purpose>
                                <Policy CarrierPartyID="Carrier_Party_1"
BankHoldingID="Bank_Holding_1">
                                        <Life>
                                                <FaceAmt>1250000</FaceAmt>
                                                <Coverage>
                                                        <LifeParticipant 
PartyID="Owner_Party_1">
                                                                
<LifeParticipantRoleCode tc="18">Owner</LifeParticipantRoleCode>
                                                        </LifeParticipant>
                                                </Coverage>
                                        </Life>
                                </Policy>
                        </Holding>
                        <Relation id="Owner_to_Policy" 
OriginatingObjectID="Proposed_Holding_1"
RelatedObjectID="Owner_Party_1">
                                <OriginatingObjectType 
tc="4">Holding</OriginatingObjectType>
                                <RelatedObjectType 
tc="6">Party</RelatedObjectType>
                                <RelationRoleCode 
tc="8">Owner</RelationRoleCode>
                        </Relation>
                </OLifE>
        </TXLifeRequest>
</TXLife>




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

Comment By: Mike Skells (mskells)
Date: 2007-08-07 22: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 22: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 17: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