Discussion:
[xquery-talk] xpath default namespace
Leo Studer
2016-05-31 16:20:24 UTC
Permalink
Hello

is there a way to declare a default namespace for xPath in xQuery 3.0 as in Schema or in XSLT?

Thanks in advance
Leo




_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Ghislain Fourny
2016-05-31 16:24:05 UTC
Permalink
Hi Leo,

In XQuery, you can do this in the prolog like so (this is from the spec):

declare default element namespace "http://example.org/names";

You can also do so using xmlns attribute in node constructors, and the
default namespace will be in scope in all nested expressions.

Kind regards,
Ghislain
Post by Leo Studer
Hello
is there a way to declare a default namespace for xPath in xQuery 3.0 as
in Schema or in XSLT?
Post by Leo Studer
Thanks in advance
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Michael Kay
2016-05-31 16:41:06 UTC
Permalink
Post by Ghislain Fourny
Hi Leo,
declare default element namespace "http://example.org/names <http://example.org/names>";
You can also do so using xmlns attribute in node constructors, and the default namespace will be in scope in all nested expressions.
Kind regards,
Ghislain
The main difference from the XSLT xpath-default-namespace is that this default applies both to names in path expressions and to names in element constructors, which is inconvenient when the input and output documents are in different namespaces.

Michael Kay
Saxonica
Leo Studer
2016-06-01 08:20:18 UTC
Permalink
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this default applies both to names in path expressions and to names in element constructors, which is inconvenient when the input and output documents are in different namespaces.
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.

Fist I tried

declare namespace null=“”;

and in the query I wrote something like

null:elementName

Then I get the error that namespace null is not declared….

The only solution I found is to put a namespace in the XML file (which is not really what I want).

Is there another way?

Cheers
Leo
_______________________________________________
Ghislain Fourny
2016-06-01 09:06:35 UTC
Permalink
Hi Leo,

If the input has no namespace, then I think you can declare the default
namespace according to your output (if it is important to you that your
output uses it as a default namespace).

Then there is a workaround to navigate the input with /*:foo/*:bar
expressions, where the joker prefix should catch the absence of namespace.

I hope it helps?

Kind regards,
Ghislain
Post by Leo Studer
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this
default applies both to names in path expressions and to names in element
constructors, which is inconvenient when the input and output documents are
in different namespaces.
Post by Michael Kay
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which is
not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Leo Studer
2016-06-01 09:38:52 UTC
Permalink
Hi Ghislain

thank you for your input, which solves my problem.

However, if I have the same element name in two different namespaces, then the use of a wildcard namespace makes problems ;-).

Always
Leo
Post by Ghislain Fourny
Hi Leo,
If the input has no namespace, then I think you can declare the default namespace according to your output (if it is important to you that your output uses it as a default namespace).
Then there is a workaround to navigate the input with /*:foo/*:bar expressions, where the joker prefix should catch the absence of namespace.
I hope it helps?
Kind regards,
Ghislain
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this default applies both to names in path expressions and to names in element constructors, which is inconvenient when the input and output documents are in different namespaces.
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which is not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Ghislain Fourny
2016-06-01 09:44:24 UTC
Permalink
Hi Leo,

I'm glad!

Should this happen, I guess you could add a filter using an XPath function
to test for the namespace of the name of the selected element.

https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name
and
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName

You can test for the absence of namespace easily.

There may be even shorter tricks, I wouldn't be surprised if Mike got
something out of his magic hat :-)

Kind regards,
Ghislain
Post by Leo Studer
Hi Ghislain
thank you for your input, which solves my problem.
However, if I have the same element name in two different namespaces, then
the use of a wildcard namespace makes problems ;-).
Always
Leo
Hi Leo,
If the input has no namespace, then I think you can declare the default
namespace according to your output (if it is important to you that your
output uses it as a default namespace).
Then there is a workaround to navigate the input with /*:foo/*:bar
expressions, where the joker prefix should catch the absence of namespace.
I hope it helps?
Kind regards,
Ghislain
Post by Leo Studer
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this
default applies both to names in path expressions and to names in element
constructors, which is inconvenient when the input and output documents are
in different namespaces.
Post by Michael Kay
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which is
not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Michael Kay
2016-06-01 11:15:18 UTC
Permalink
If the input and output are both in a namespace, and you want the output to be in a namespace but with no namespace prefix, then I think your options are:

(a) Use *:name in path expressions (which may over-select)

(b) Bind a prefix p to the input namespace and use p:name in path expressions (which is tedious)

(c) Use Q{}name in path expressions (which is 3.0 only, and tedious)

(d) Use computed element constructors, and binding the default namespace to the input namespace

(e) Use XSLT 2.0 with the default-xpath-namespace attribute.

Sadly, I remember trying to persuade the XQuery WG to allow different default namespaces for input and output element names, and the general reaction was "Mike, why do you keep trying to make namespaces even more complicated than they are already?". So XQuery repeated the mistake of XSLT 1.0, only in a slightly different form.

Handling input documents with a default namespace remains by a good measure the #1 xpath usability problem, a new StackOverflow user falls into the trap nearly every day.

Michael Kay
Saxonica
Post by Ghislain Fourny
Hi Leo,
I'm glad!
Should this happen, I guess you could add a filter using an XPath function to test for the namespace of the name of the selected element.
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name <https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name>
and
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName <https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName>
You can test for the absence of namespace easily.
There may be even shorter tricks, I wouldn't be surprised if Mike got something out of his magic hat :-)
Kind regards,
Ghislain
Hi Ghislain
thank you for your input, which solves my problem.
However, if I have the same element name in two different namespaces, then the use of a wildcard namespace makes problems ;-).
Always
Leo
Post by Ghislain Fourny
Hi Leo,
If the input has no namespace, then I think you can declare the default namespace according to your output (if it is important to you that your output uses it as a default namespace).
Then there is a workaround to navigate the input with /*:foo/*:bar expressions, where the joker prefix should catch the absence of namespace.
I hope it helps?
Kind regards,
Ghislain
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this default applies both to names in path expressions and to names in element constructors, which is inconvenient when the input and output documents are in different namespaces.
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which is not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Leo Studer
2016-06-05 21:15:06 UTC
Permalink
Thank you Mike And Ghislain

may be xPath/xQuery 3.2 will have the possibility of different default namespaces for input and output elements ;-)
and also the possibility to bind a prefix to the null namespace (“”) ...

Always
Leo
Post by Michael Kay
(a) Use *:name in path expressions (which may over-select)
(b) Bind a prefix p to the input namespace and use p:name in path expressions (which is tedious)
(c) Use Q{}name in path expressions (which is 3.0 only, and tedious)
(d) Use computed element constructors, and binding the default namespace to the input namespace
(e) Use XSLT 2.0 with the default-xpath-namespace attribute.
Sadly, I remember trying to persuade the XQuery WG to allow different default namespaces for input and output element names, and the general reaction was "Mike, why do you keep trying to make namespaces even more complicated than they are already?". So XQuery repeated the mistake of XSLT 1.0, only in a slightly different form.
Handling input documents with a default namespace remains by a good measure the #1 xpath usability problem, a new StackOverflow user falls into the trap nearly every day.
Michael Kay
Saxonica
Post by Ghislain Fourny
Hi Leo,
I'm glad!
Should this happen, I guess you could add a filter using an XPath function to test for the namespace of the name of the selected element.
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name <https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name>
and
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName <https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName>
You can test for the absence of namespace easily.
There may be even shorter tricks, I wouldn't be surprised if Mike got something out of his magic hat :-)
Kind regards,
Ghislain
Hi Ghislain
thank you for your input, which solves my problem.
However, if I have the same element name in two different namespaces, then the use of a wildcard namespace makes problems ;-).
Always
Leo
Post by Ghislain Fourny
Hi Leo,
If the input has no namespace, then I think you can declare the default namespace according to your output (if it is important to you that your output uses it as a default namespace).
Then there is a workaround to navigate the input with /*:foo/*:bar expressions, where the joker prefix should catch the absence of namespace.
I hope it helps?
Kind regards,
Ghislain
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this default applies both to names in path expressions and to names in element constructors, which is inconvenient when the input and output documents are in different namespaces.
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which is not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk <http://x-query.com/mailman/listinfo/talk>
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Graydon Saunders
2016-06-05 21:20:13 UTC
Permalink
And just possibly some way to flush the haze of unwanted namespaces
clinging to the query result, too.

-- Graydon
Post by Leo Studer
Thank you Mike And Ghislain
may be xPath/xQuery 3.2 will have the possibility of different default
namespaces for input and output elements ;-)
and also the possibility to bind a prefix to the null namespace (“”) ...
Always
Leo
If the input and output are both in a namespace, and you want the output
to be in a namespace but with no namespace prefix, then I think your
(a) Use *:name in path expressions (which may over-select)
(b) Bind a prefix p to the input namespace and use p:name in path
expressions (which is tedious)
(c) Use Q{}name in path expressions (which is 3.0 only, and tedious)
(d) Use computed element constructors, and binding the default namespace
to the input namespace
(e) Use XSLT 2.0 with the default-xpath-namespace attribute.
Sadly, I remember trying to persuade the XQuery WG to allow different
default namespaces for input and output element names, and the general
reaction was "Mike, why do you keep trying to make namespaces even more
complicated than they are already?". So XQuery repeated the mistake of XSLT
1.0, only in a slightly different form.
Handling input documents with a default namespace remains by a good
measure the #1 xpath usability problem, a new StackOverflow user falls into
the trap nearly every day.
Michael Kay
Saxonica
Hi Leo,
I'm glad!
Should this happen, I guess you could add a filter using an XPath function
to test for the namespace of the name of the selected element.
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-node-name
and
https://www.w3.org/XML/Group/qtspecs/specifications/xpath-functions-30/html/Overview.html#func-namespace-uri-from-QName
You can test for the absence of namespace easily.
There may be even shorter tricks, I wouldn't be surprised if Mike got
something out of his magic hat :-)
Kind regards,
Ghislain
Post by Leo Studer
Hi Ghislain
thank you for your input, which solves my problem.
However, if I have the same element name in two different namespaces,
then the use of a wildcard namespace makes problems ;-).
Always
Leo
Hi Leo,
If the input has no namespace, then I think you can declare the default
namespace according to your output (if it is important to you that your
output uses it as a default namespace).
Then there is a workaround to navigate the input with /*:foo/*:bar
expressions, where the joker prefix should catch the absence of namespace.
I hope it helps?
Kind regards,
Ghislain
Post by Leo Studer
Hi Michael
Post by Michael Kay
The main difference from the XSLT xpath-default-namespace is that this
default applies both to names in path expressions and to names in element
constructors, which is inconvenient when the input and output documents are
in different namespaces.
Post by Michael Kay
Michael Kay
Saxonica
this is exactly my problem. The XML file has no namespace and the output
has a namespace.
Fist I tried
declare namespace null=“”;
and in the query I wrote something like
null:elementName
Then I get the error that namespace null is not declared
.
The only solution I found is to put a namespace in the XML file (which
is not really what I want).
Is there another way?
Cheers
Leo
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
_______________________________________________
http://x-query.com/mailman/listinfo/talk
Liam R. E. Quin
2016-06-06 06:30:33 UTC
Permalink
Post by Leo Studer
Thank you Mike And Ghislain
may be  xPath/xQuery 3.2 will have 
I'm not expecting to see an XQuery 3.2 or XPath 3.2.

I could always be surprised, there could be a sudden surge of interest
and resources, but rightnow it's not looking likely.

Liam
--
Liam R. E. Quin <***@w3.org>
The World Wide Web Consortium (W3C)
_______________________________________________
***@x-query.com
http://
Continue reading on narkive:
Loading...