Title: RE: [dom4j-user] Parsing namespaces .... how d'you do it?
I've been using DOM4J for a while now, and this compliance really bit me in the arse.
I've looked in the mailing list logs, and the pro's and con's have been debated, and the end result was a win for  absolute XPath spec compliance over useability.
 
As a suggestion what if there was a 'shadow' set of dom4j functionality that DID understand default namespace binding.
 
Let's call it _XtraPath_.  So Raymond would have used
 
selectSingleNodeXtraPath("//PensionSchemeDetails/PensionSchemeRegistryNumber"); 
 
This would understand that it should bind any unbound element to an internal anonymous namespace _foo_ which  is not present in the document . It would alter the XtraPath to use that _foo_ namespace. It would return any anonymously bound elements which matched.
 
So there would still be absolute compliance with the regular methods, but the convenience would still be there. Would this ever be acceptable to the DOM4J team??
 
 
Reason:
 
The real pain caused by strict compliance is having to convert all your malformed content to have this prefix. If you have a lot of legacy XML/SVG/Custom stuff and want to use it you cannot, unless you convert it.
 
Then if you are authoring content, say by hand to put on _any_ prefix is a real pain. At least two characters 'a:' per open tag, attribute and closing tag. It's a recipe for error and frustration, especially if you don't have a real time validation tool, such as uploading content to a webapp that needs deployment time.
 
It is less terse, and harder to validate by eye. And when push comes to shove, when there is a bug, you have to validate by eye.
 
Also, newcomers clearly expect this functionality and it confuses them that there is no namespace binding by default.
 
Terry.

From: Priest, Mark [mailto:[EMAIL PROTECTED]
Sent: Tue 17/05/2005 16:44
To: 'Offiah Raymond, Slough'; dom4j-user@lists.sourceforge.net
Subject: RE: [dom4j-user] Parsing namespaces .... how d'you do it?

Raymond,

You have to include namespace references in the xpath expressions.  Also,
xpath does not handle using the default namespace so you will have to define
a prefix for the
http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regulation
namespace.  You can either do this in code or in the source document.  Here
is an example if you do it in code:

            document.getRootElement().add(DocumentHelper.createNamespace(
                    "prefix",
"
http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regulation
"));

        Node registyNumber =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:PensionSchem
eRegistryNumber");
        Node name =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Name");
        Node addressLine1 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[1]");
        Node addressLine2 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[2]");
        Node addressLine3 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[3]");
        Node addressLine4 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[4]");
        Node addressLine5 =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:Line[5]");
        Node postcode =
document.selectSingleNode("//prefix:PensionSchemeDetails/prefix:Address/pref
ix:UKaddress/govADP:PostCode");


Good luck,
Mark

-----Original Message-----
From: Offiah Raymond, Slough [
mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 17, 2005 11:27 AM
To: dom4j-user@lists.sourceforge.net
Subject: [dom4j-user] Parsing namespaces .... how d'you do it?


Hello there.

I wonder if someone could help me out here.

I'm trying to parse an XML file, using DOM4J/XPath, and I'm having trouble
with the namespaces defined in the file:

<PensionSchemeReturnRequest
 
xmlns="
http://www.pensions.gov.uk/EconomicsAndFinance/Personal/Pensions/Regu
lation"
 
xmlns:govADP="
http://www.pensions.gov.uk/people/AddressAndPersonalDetails"
    xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 
xsi:schemaLocation="
http://www.pensions.gov.uk/EconomicsAndFinance/Personal/
Pensions/Regulation \OccupationalPensionSchemeReturnRequest-v1.3.xsd"
    regulatoryReference="57437337">
  <PensionSchemeDetails>
    <PensionSchemeRegistryNumber>10000047</PensionSchemeRegistryNumber>
    <Name>John Smith</Name>
    <Address AddressUsage="work">
      <UKaddress>
        <govADP:Line>Code House</govADP:Line>
        <govADP:Line>Smith Streett</govADP:Line>
        <govADP:Line>Deaconsfield</govADP:Line>
        <govADP:PostCode>DE1 TR5P</govADP:PostCode>
      </UKaddress>
    </Address>
  </PensionSchemeDetails>
</PensionSchemeReturnRequest>

... with this bit of code:

        Node registyNumber =
document.selectSingleNode("//PensionSchemeDetails/PensionSchemeRegistryNumbe
r");
        Node name =
document.selectSingleNode("//PensionSchemeDetails/Name");
        Node addressLine1 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[1]"
);
        Node addressLine2 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[2]"
);
        Node addressLine3 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[3]"
);
        Node addressLine4 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[4]"
);
        Node addressLine5 =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/Line[5]"
);
        Node postcode =
document.selectSingleNode("//PensionSchemeDetails/Address/UKaddress/PostCode
");

If I strip out all the namespace stuff, it works OK.

How do I parse XML files that are namespaced to the hilt ....

Thanks in advance

Ray


Scanned for viruses by MessageLabs. The integrity and security of this
message cannot be guaranteed. This email is intended for the named recipient
only, and may contain confidential information and proprietary material. Any
unauthorised use or disclosure is prohibited.


-------------------------------------------------------
This SF.Net email is sponsored by Oracle Space Sweepstakes
Want to be the first software developer in space?
Enter now for the Oracle Space Sweepstakes!
http://ads.osdn.com/?ad_id=7412&alloc_id=16344&op=click
_______________________________________________
dom4j-user mailing list
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user



This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify Adgistics Ltd by replying to the original sender of this mail. This message may contain confidential information and is intended only for the individual named. If you are not the named addressee you should delete this e-mail and not disseminate, distribute or copy this e-mail.

Reply via email to