Pascal,
in
<a xmlns="mynamespace">
     <b/>
</a>
what you have is every element within and including a being qualified as
having the namespace "mynamespace". Thus, the XPath must qualify the
elements too. This is only logical.
There are a couple of ways you can qualify the elements in the XPath.
You can use the prolog you have in
"declare namespace xs='mynamespace';/xs:a/xs:b"; you can also use
"declare default namespace 'mynamespace; /a/b".
The bottom line is that the path must follow the structure of the
document. If elements/attributes are namespace-qualified in the
document, then they must be too in the path.
- Wing Yew


________________________________

From: Pascal Maugeri [mailto:[EMAIL PROTECTED] 
Sent: Friday, February 22, 2008 8:40 AM
To: user@xmlbeans.apache.org
Subject: Applying XPath to an XML with or without namespace


Hi

I would like to have some clarifications on how to apply XPath query on
a XML document depending if it comes with or without a namespace
definition.

When I run the following code executing the XPath query /a/b on a
document with no namespace:

        String doc = "<a><b/></a>";
        String query = "/a/b";
        XmlObject xml = XmlObject.Factory.parse(doc);
        XmlCursor cursor = xml.newCursor();
        cursor.push();
        cursor.selectPath(query);

it gives me the correct result (<c/>).

But if if the document comes with a namespace, for instance:

<a xmlns="mynamespace">
     <b/>
</a>

the same code does not give me any result.

After investigating I found that a working code could be:

        String doc = "<a xmlns="mynamespace"><b/></a>";
        String query = "declare namespace xs='mynamespace';/xs:a/xs:b";
        XmlObject xml = XmlObject.Factory.parse(doc);
        XmlCursor cursor = xml.newCursor();
        cursor.push();
        cursor.selectPath(query);

Is there a better way to do this without having to set the prefix "xs:"
in the Xpath query for each level ? I would like to find a generic way
to handle XML documents with or without namespace.

Regards,
Pascal


Notice:  This email message, together with any attachments, may contain 
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated 
entities,  that may be confidential,  proprietary,  copyrighted  and/or legally 
privileged, and is intended solely for the use of the individual or entity 
named in this message. If you are not the intended recipient, and have received 
this message in error, please immediately return this by email and then delete 
it.

Reply via email to