Andreas -

Thanks for the information.

I found this example showing how to build queries for default namespaces in 
Jaxen:

http://jaxen.codehaus.org/faq.html

Mapping that to AXIOMXPath:

SimpleNamespaceContext context = new SimpleNamespaceContext();
context.addNamespace("pre", uri);

String xpathString = "/pre:X/pre:Y/pre:Z";
AXIOMXPath xpathExpression = new AXIOMXPath(xpathString);
xpathExpression.setNamespaceContext(context);

And that brings back the expected OMElement.

Thanks.


Robert

-----Original Message-----
From: Andreas Veithen [mailto:andreas.veit...@gmail.com] 
Sent: 22 October 2009 19:41
To: axis-user@ws.apache.org
Subject: Re: Problems using XPath in Axiom when namespaces are involved

Robert,

My guess is that the issue is related to the fact that you are trying
to define a default namespace for the XPath expression. Can you check
in the Jaxen documentation if it says something about this?

Note that defining a default namespace for an XPath expression is not
a good idea anyway, because it will prevent you from matching elements
or attributes in the null namespace. The reason is that the null
namespace can only be used as default namespace (i.e. xmlns:p="" is
illegal). Therefore if you declare a different default namespace, you
can't refer to the null namespace in your XPath expression.

That is the reason why for example the XSLT specification defines the
namespace context of an XPath expression as follows: "the set of
namespace declarations are those in scope on the element which has the
attribute in which the expression occurs; [...] the default namespace
(as declared by xmlns) is not part of this set"

Andreas

On Thu, Oct 22, 2009 at 19:09, Robert Baldock <robert.bald...@pfiks.com> wrote:
> Hi -
>
> I'm having problems getting my head round the use of XPath in Axiom,
> especially when using namespaces.
>
> With an XML snippet like this:
>
> <?xml version="1.0" encoding="utf-16"?>
> <X>
>  <Y>
>    <Z>test</Z>
>  </Y>
> </X>
>
> And a code snippet like this:
>
> String xpathString = "/X/Y/Z";
> AXIOMXPath xpathExpression = new AXIOMXPath(xpathString);
> OMElement element = (OMElement)xpathExpression.selectSingleNode(root);
>
> I can retrieve the element from the XML without any problem.
>
> However, with XML like this:
>
> <?xml version="1.0" encoding="utf-16"?>
> <X xmlns:xsi="A " xmlns:xsd="B" xmlns="C">
>  <Y>
>    <Z>test</Z>
>  </Y>
> </X>
>
> OMNamespace namespace = documentElement.getNamespace();
> String prefix = namespace.getPrefix();
> String uri = namespace.getNamespaceURI();
>
> AXIOMXPath xpathExpression = new AXIOMXPath(xpathString);
> xpathExpression.addNamespace(prefix, uri);
> OMElement element = (OMElement)xpathExpression.selectSingleNode(root);
>
> I get no results.
>
> Can anyone spot why this might be?
>
> Any advice would be much appreciated.
>
>
> Robert
>
>
>

Reply via email to