Fwd: Select XPath with namespace (Prefix has not been declared)

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Select XPath with namespace (Prefix has not been declared)

Marc Dzaebel
Dear Michael,

I'm evaluating an XPath using net.sf.saxon.s9api.Processor.newXPathCompiler() getting the "Prefix n has not been declared" exception with XPath "n:b" for
   <a xmlns:n='..'><b>2</b><n:b>4</n:b></a>

Using compiler.declareNamespace("n", "..") eliminates the problem, but isn't there a simple way to automatically declare all namespaces, defined in the source? I guess, that usually many would expect that "n:b" is selectable by default. Is there a simple but complete example of how to do this? A problem in my view is, that the declaration has to be done before compilation so after loading a source, the compilation has to be redone. In previous help requests about this topic you mentioned, that it's possible to find namespaces by element.iterateAxis(Axis.NAMESPACE) but do I really have to load the document, gather all namespaces, declare them, recompile the xpath and then evalute? This would be quite expensive for a simple evaluation.

Thanks a lot in advance and best regards

Marc Dzaebel

Germany, Lüneburg


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Select XPath with namespace (Prefix has not been declared)

Martin Honnen-2
Marc Dzaebel wrote:

> /Dear Michael,//
> /
> //
> I'm evaluating an XPath using
> net.sf.saxon.s9api.Processor.newXPathCompiler() getting the "/Prefix n
> has not been declared/" exception with XPath "n:b" for
> <a xmlns:n='..'><b>2</b><*n:b*>4</*n:b*></a>
>
> Using compiler.declareNamespace("n", "..") eliminates the problem, but
> isn't there a simple way to automatically declare all namespaces,
> defined in the source?

What kind of automation of the declaration of namespaces would you
expect for cases like

<foo xmlns="http://example.com/ns1"><bar
xmlns="http://example.com/ns2">...</bar></foo>

or

<pf:foo xmlns:pf="http://example.com/ns1"><pf:bar
xmlns:pf="http://example.com/ns2">...</pf:bar></pf:foo>

?



------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Select XPath with namespace (Prefix has not been declared)

Michael Kay
In reply to this post by Marc Dzaebel
You could try ((IndependentContext)xpath.getUnderlyingStaticContext()).setNamespaces(node) where node is the outermost element in the source document. Doesn’t work for namespaces that aren’t declared at the top level: you can’t do that in the general case because there might be multiple bindings for different prefixes (especially, of course, the default prefix).

The question really is, why would you want to? Different source documents might bind different prefixes to the same namespace - when you write your XPath expressions, you don’t want them to be dependent on the prefixes chosen by the document author. Just choose your own prefixes, and declare them.

Michael Kay
Saxonica


On 16 May 2015, at 13:15, Marc Dzaebel <[hidden email]> wrote:

Dear Michael,

I'm evaluating an XPath using net.sf.saxon.s9api.Processor.newXPathCompiler() getting the "Prefix n has not been declared" exception with XPath "n:b" for
   <a xmlns:n='..'><b>2</b><n:b>4</n:b></a>

Using compiler.declareNamespace("n", "..") eliminates the problem, but isn't there a simple way to automatically declare all namespaces, defined in the source? I guess, that usually many would expect that "n:b" is selectable by default. Is there a simple but complete example of how to do this? A problem in my view is, that the declaration has to be done before compilation so after loading a source, the compilation has to be redone. In previous help requests about this topic you mentioned, that it's possible to find namespaces by element.iterateAxis(Axis.NAMESPACE) but do I really have to load the document, gather all namespaces, declare them, recompile the xpath and then evalute? This would be quite expensive for a simple evaluation.

Thanks a lot in advance and best regards

Marc Dzaebel

Germany, Lüneburg

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Select XPath with namespace (Prefix has not been declared)

Marc Dzaebel
In reply to this post by Martin Honnen-2
Dear Michael (and Martin),

thanks for the hints about ambiguity of URI mappings per prefix, especially for multiple sources. However, an XPath like "prefix:tag" should in my view match any nodes with that prefix, regardless of different URI mappings (against Spec). Of course it's possible to use "Q{uri}:tag" to find nodes by their namespace URI and this might be the safest/recommended way to select what you really intended. However, for trivial usecases like reading single documents, it might be helpfull to be able to use prefixes just because they are (usually) shorter, which matters in many applications. The change would be, that you don't find the mapped URI but rather only the prefix. Practically XPaths would be more powerful with this interpretation and there could be circumstances where this is really wanted, even if the mapped URI's differ. I'm far away from being able to judge the impact of such a change or whether this is simply impossible for other reasons, but it could be an expectation of useres who enter the field (even if this would need a specification change).

I tried your code but the prefix error remains as compile("p:t") already needs the namespace only to parse the XPath. So, as suspected, in order to be able to select prefixes, it's necessary to recompile XPaths, isn't it?

However, I learned a lot from your response and will use URI's rather than prefixes.

Thank you so much especially for all your continuous, invaluable Saxon and specification work!

Marc

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Select XPath with namespace (Prefix has not been declared)

John Lumley-2
On 17/05/2015 12:10, Marc Dzaebel wrote:
However, an XPath like "prefix:tag" should in my view match any nodes with that prefix, regardless of different URI mappings (against Spec). Of course it's possible to use "Q{uri}:tag" to find nodes by their namespace URI and this might be the safest/recommended way to select what you really intended. However, for trivial usecases like reading single documents, it might be helpfull to be able to use prefixes just because they are (usually) shorter, which matters in many applications. The change would be, that you don't find the mapped URI but rather only the prefix. Practically XPaths would be more powerful with this interpretation and there could be circumstances where this is really wanted, even if the mapped URI's differ. I'm far away from being able to judge the impact of such a change or whether this is simply impossible for other reasons, but it could be an expectation of useres who enter the field (even if this would need a specification change).

I would suggest you read Michael Kay's remarks on page 836 of his 4th Edition XSLT 2.0 and XPath 2.0, where he discusses usage of name() and the implications around namespace prefixes.

--
John Lumley MA PhD CEng FIEE
[hidden email]
on behalf of Saxonica Ltd

------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Select XPath with namespace (Prefix has not been declared)

Michael Kay
In reply to this post by Marc Dzaebel

> On 17 May 2015, at 12:10, Marc Dzaebel <[hidden email]> wrote:
>
> >However, an XPath like "prefix:tag" should in my view match any nodes with that prefix, regardless of different URI mappings (against Spec).

Unfortunately the spec says what it says, and deviating from it (or interpreting it too “creatively”) would only cause yet more confusion. The problem of binding to names in a namespace in XPath (especially the default namespace) is certainly the #1 usability problem in the language, especially for beginners; however, it’s one of these areas where once you learn the rules, and stop beating your head against a wall because you wish the rules were different, it’s not actually difficult to achieve what you want.

>
> I tried your code but the prefix error remains as compile("p:t") already needs the namespace only to parse the XPath. So, as suspected, in order to be able to select prefixes, it's necessary to recompile XPaths, isn't it?
>

Yes, any prefixes used in QNames in the XPath expression need to be bound to a known namespace at compile-time, and the binding cannot be changed without compiling from scratch. (In the language of the spec, the prefix/namespace bindings are part of the static context of the expression)

Michael Kay
Saxonica


------------------------------------------------------------------------------
One dashboard for servers and applications across Physical-Virtual-Cloud
Widest out-of-the-box monitoring support with 50+ applications
Performance metrics, stats and reports that give you Actionable Insights
Deep dive visibility with transaction tracing using APM Insight.
http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help 
Reply | Threaded
Open this post in threaded view
|

Re: Select XPath with namespace (Prefix has not been declared)

Вячеслав Седов
maybe *:p "can save the father of russian revolution"?

2015-05-22 1:10 GMT+05:00 Michael Kay <[hidden email]>:

>
>> On 17 May 2015, at 12:10, Marc Dzaebel <[hidden email]> wrote:
>>
>> >However, an XPath like "prefix:tag" should in my view match any nodes with that prefix, regardless of different URI mappings (against Spec).
>
> Unfortunately the spec says what it says, and deviating from it (or interpreting it too “creatively”) would only cause yet more confusion. The problem of binding to names in a namespace in XPath (especially the default namespace) is certainly the #1 usability problem in the language, especially for beginners; however, it’s one of these areas where once you learn the rules, and stop beating your head against a wall because you wish the rules were different, it’s not actually difficult to achieve what you want.
>
>>
>> I tried your code but the prefix error remains as compile("p:t") already needs the namespace only to parse the XPath. So, as suspected, in order to be able to select prefixes, it's necessary to recompile XPaths, isn't it?
>>
>
> Yes, any prefixes used in QNames in the XPath expression need to be bound to a known namespace at compile-time, and the binding cannot be changed without compiling from scratch. (In the language of the spec, the prefix/namespace bindings are part of the static context of the expression)
>
> Michael Kay
> Saxonica
>
>
> ------------------------------------------------------------------------------
> One dashboard for servers and applications across Physical-Virtual-Cloud
> Widest out-of-the-box monitoring support with 50+ applications
> Performance metrics, stats and reports that give you Actionable Insights
> Deep dive visibility with transaction tracing using APM Insight.
> http://ad.doubleclick.net/ddm/clk/290420510;117567292;y
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

------------------------------------------------------------------------------
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[hidden email]
https://lists.sourceforge.net/lists/listinfo/saxon-help