XML file with xmlns attribute and XPATH

Thursday, 24 May 2012 · 7 messages · last reply 26 May 2012
PR
Philippe Rathé24 May 2012 06:20 #
Hello there, I just want to do a simple xpath expression but I can't get it works when I use this XML document
<?xml version="1.0" encoding='utf-8'?>
<feed xmlns="http://example.org" xmlns:abc="http://abc.example.org">
  <title>Projects</title>
</feed>
The expression "exquery //title" does not return anything from the CLI once I open the database containing only that file. But it works with this file
<?xml version="1.0" encoding='utf-8'?>
<feed xmlns="http://example.org">
  <title>Projects</title>
</feed>
It looks like having both xmlns and xmlns:abc namespace attribute creates a problem. My real use case is atompub feed document which the 2 first lines looks like this:
<?xml version="1.0" encoding='utf-8'?>
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app">
Any idea? I'm using a 7.2.2 snapshot, thanks.
MS
Michael Seiferle24 May 2012 10:46 #
Hi Philippe, you still have to declare a default element namespace for XQuery (or XPath for that matter) to recognize the element you want to return:
Quoted text · Philippe Rathé · 4 lines
> declare default element namespace "http://example.org"; > <feed xmlns="http://example.org" xmlns:abc="http://abc.example.org"> > <title>Projects</title> > </feed>/title
will work as expected. I see that the solution is not obvious, but from the processors point of view: * the XML fragment has the http://example.org namespace * your path expressions don't. I modified your example slightly to show an alternative way of doing this:
Quoted text · Philippe Rathé · 5 lines
> declare namespace a = "http://example.org"; > declare namespace b = "http://abc.example.org"; > <feed xmlns="http://example.org" xmlns:abc="http://abc.example.org"> > <title abc:test="foo">Projects</title> > </feed>/a:title[@b:test = "foo"]
Feel free to ask for more help when needed, Michael
Quoted text · Philippe Rathé · 37 lines
Am 24.05.2012 um 04:20 schrieb Philippe Rathé: > Hello there, > > I just want to do a simple xpath expression but I can't get it works > when I use this XML document > > <?xml version="1.0" encoding='utf-8'?> > <feed xmlns="http://example.org" xmlns:abc="http://abc.example.org"> > <title>Projects</title> > </feed> > > The expression "exquery //title" does not return anything from the CLI > once I open the database containing only that file. > > But it works with this file > > <?xml version="1.0" encoding='utf-8'?> > <feed xmlns="http://example.org"> > <title>Projects</title> > </feed> > > It looks like having both xmlns and xmlns:abc namespace attribute > creates a problem. > My real use case is atompub feed document which the 2 first lines > looks like this: > > <?xml version="1.0" encoding='utf-8'?> > <feed xmlns="http://www.w3.org/2005/Atom" > xmlns:app="http://www.w3.org/2007/app"> > > Any idea? > I'm using a 7.2.2 snapshot, > thanks. > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
CG
Christian Grünin reply to Philippe Rathé, 24 May 2012, 06:2024 May 2012 17:58 #
Quoted text · Philippe Rathé · 6 lines
> But it works with this file > > <?xml version="1.0" encoding='utf-8'?> > <feed xmlns="http://example.org"> > <title>Projects</title> > </feed>
...indeed it shouldn't work, as Michael already pointed out. But you are lucky; this issue has recently been fixed due to a bug report from Andy Bunce, which means that it should work again with the latest snapshot. Btw, if you know that there's only one namespace in your document, you may as well use the wildcard instead of fully writing down your namespace: //*:feed C.
PR
Philippe Rathé25 May 2012 06:23 #
Christian and Michael, indeed I'm overwhelmed by those new technologies. I just overlooked XML namespace! Thanks for your help. -- Philippe
Quoted text · Christian Grün · 21 lines
On Thu, May 24, 2012 at 11:58 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> But it works with this file >> >> <?xml version="1.0" encoding='utf-8'?> >> <feed xmlns="http://example.org"> >> <title>Projects</title> >> </feed> > > ...indeed it shouldn't work, as Michael already pointed out. But you > are lucky; this issue has recently been fixed due to a bug report from > Andy Bunce, which means that it should work again with the latest > snapshot. > > Btw, if you know that there's only one namespace in your document, you > may as well use the wildcard instead of fully writing down your > namespace: > > //*:feed > > C.
CG
Christian Grünin reply to Philippe Rathé, 25 May 2012, 16:5925 May 2012 15:04 #
Quoted text · Philippe Rathé · 2 lines
> Do I really need to declare a default namespace or can I just use > namespace everywhere in my code and it will be ok?
Defining a default namespace is just one option; it's completely ok to declare namespace and attach them to prefixes. You may as well use the new XQuery 3.0 notation: Q{http://my.namespace.org/optional/path}my-function() ... Hth, C.
PR
Philippe Rathéin reply to Philippe Rathé, 25 May 2012, 06:2325 May 2012 16:59 #
Michael, Do I really need to declare a default namespace or can I just use namespace everywhere in my code and it will be ok? Will declaring a default namespace impact my existing code such as everything which is not prefixed by namespace like function variable or function call? Thanks, -- Philippe
Quoted text · Philippe Rathé · 30 lines
On Thu, May 24, 2012 at 10:23 PM, Philippe Rathé <prathe at gmail.com> wrote: > Christian and Michael, > > indeed I'm overwhelmed by those new technologies. I just overlooked > XML namespace! > Thanks for your help. > > -- Philippe > > On Thu, May 24, 2012 at 11:58 AM, Christian Grün > <christian.gruen at gmail.com> wrote: >>> But it works with this file >>> >>> <?xml version="1.0" encoding='utf-8'?> >>> <feed xmlns="http://example.org"> >>> <title>Projects</title> >>> </feed> >> >> ...indeed it shouldn't work, as Michael already pointed out. But you >> are lucky; this issue has recently been fixed due to a bug report from >> Andy Bunce, which means that it should work again with the latest >> snapshot. >> >> Btw, if you know that there's only one namespace in your document, you >> may as well use the wildcard instead of fully writing down your >> namespace: >> >> //*:feed >> >> C.
PR
Philippe Rathéin reply to Christian Grün, 25 May 2012, 15:0426 May 2012 04:55 #
I've read on namespaces and tried a default namespace on element as Michael suggested
    declare default element namespace "http://www.w3.org/2005/Atom";
But RESTXQ raise an error that I didn't have before I declared it: [REXQ9999] Variable $category is not specified as argument. In fact it seems to raise on the first function that use a variable in the annotation. The variable is really passed as an argument, but I suspect that the argument is in my default namespace and so it is not "seen" by RESTXQ. This is the function that created the error: declare %rest:path("/categories/{$category}") %rest:GET %rest:produces("application/atomcat+xml") %output:omit-xml-declaration("no") %output:media-type("application/atomcat+xml") function page:category($category as xs:string) { Am I forgetting something? Thanks, -- Philippe
Quoted text · Christian Grün · 12 lines
On Fri, May 25, 2012 at 9:04 AM, Christian Grün <christian.gruen at gmail.com> wrote: >> Do I really need to declare a default namespace or can I just use >> namespace everywhere in my code and it will be ok? > > Defining a default namespace is just one option; it's completely ok to > declare namespace and attach them to prefixes. You may as well use the > new XQuery 3.0 notation: > > Q{http://my.namespace.org/optional/path}my-function() ... > > Hth, C.

⚡Generated with XQuery