xquery of attributes
Thursday, 7 May 2015 · 4 messages
FG
Florent Gallaire7 May 2015 02:18 #
In the RESTXQ documentation there is this example which works fine :
declare
%rest:path("cities")
%output:method("json")
%output:json("format=jsonml")
function page:cities()
{
element cities {
db:open('factbook')//city/name
}
};
If I have the same factbook db but with name as attribute and not as element, with the only change in the code the xquery request db:open('factbook')//city/@name I will have the error message: [XQDY0025] Duplicate attribute 'name'.
That's ok, but what is the right way in xquery to easily use multiple attributes as return value of request.
Is the right way to prefer elements to attributes ?
Cheers
Florent
--
FLOSS Engineer & Lawyer
FG
Florent Gallaire7 May 2015 03:38 #
I answer to myself :
db:open('factbook')//city/@name/string()
will works fine.
Still, is there a best way ? Is there some problems with that ? Is the element way better for xquery web usage ?
Cheers
Florent
Quoted text · Florent Gallaire · 32 lines
On Thu, May 7, 2015 at 2:18 AM, Florent Gallaire <fgallaire at gmail.com> wrote: > In the RESTXQ documentation there is this example which works fine : > > declare > %rest:path("cities") > %output:method("json") > %output:json("format=jsonml") > function page:cities() > { > element cities { > db:open('factbook')//city/name > } > }; > > If I have the same factbook db but with name as attribute and not as > element, with the only change in the code the xquery request > db:open('factbook')//city/@name I will have the error message: > [XQDY0025] Duplicate attribute 'name'. > > That's ok, but what is the right way in xquery to easily use multiple > attributes as return value of request. > > Is the right way to prefer elements to attributes ? > > Cheers > > Florent > > > > -- > FLOSS Engineer & Lawyer
--
FLOSS Engineer & Lawyer
DK
Dirk Kirsten7 May 2015 09:26 #
Hello Florent,
first of all, it doesn't matter here whether you use XQuery in a web context or not - It would throw the same error even without using RESTXQ.
The problem with your first error was that you selected multiple attributes and constructed and element with this - As an XML element can not have multiple attributes with the same name, this will not work. Thats why it does work with elements, because each element can have multiple child elements with the same name. In your proposed solution, string() converts the attributes to plain strings, and of course your element can hav a text child.
So your solution is very much ok, but "what the best way" is enterily depends on what you want as output. If the current solution gives you your desired ouput, then I see no harm in using this one. However, if it is not and you want more help, it would be nice if you could provide a small example output on what you want to get back as result.
Cheers
Dirk
Quoted text · Florent Gallaire · 47 lines
On 05/07/2015 03:38 AM, Florent Gallaire wrote: > I answer to myself : > db:open('factbook')//city/@name/string() > will works fine. > > Still, is there a best way ? Is there some problems with that ? Is the > element way better for xquery web usage ? > > Cheers > > Florent > > On Thu, May 7, 2015 at 2:18 AM, Florent Gallaire <fgallaire at gmail.com> wrote: > > In the RESTXQ documentation there is this example which works fine : > > > > declare > > %rest:path("cities") > > %output:method("json") > > %output:json("format=jsonml") > > function page:cities() > > { > > element cities { > > db:open('factbook')//city/name > > } > > }; > > > > If I have the same factbook db but with name as attribute and not as > > element, with the only change in the code the xquery request > > db:open('factbook')//city/@name I will have the error message: > > [XQDY0025] Duplicate attribute 'name'. > > > > That's ok, but what is the right way in xquery to easily use multiple > > attributes as return value of request. > > > > Is the right way to prefer elements to attributes ? > > > > Cheers > > > > Florent > > > > > > > > -- > > FLOSS Engineer & Lawyer > > >
--
Dirk Kirsten, BaseX GmbH, http://basexgmbh.de
|-- Firmensitz: Blarerstrasse 56, 78462 Konstanz
|-- Registergericht Freiburg, HRB: 708285, Geschäftsführer:
| Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22
FG
Florent Gallaire7 May 2015 14:48 #
Quoted text · Dirk Kirsten · 1 line
> So your solution is very much ok, but "what the best way" is enterily depends on what you want as output. If the current solution gives you your desired ouput, then I see no harm in using this one. However, if it is not and you want more help, it would be nice if you could provide a small example output on what you want to get back as result.
My first constraint is to return a valid XML document, so with a root element, for an easy clients-side processing. Multiple elements return value inside a root element works fine, and its IMO easier than deal with multiple attributes return value.
The string() works fine for me because in this case my attributes don't have whitespaces, but this could be more complex to handle if they had.
Dirk, once again, thanks a lot for your answer ! My project is slowly going forward thanks of BaseX.
Cheers
Florent
Quoted text · Dirk Kirsten · 57 lines
> Cheers > Dirk > On 05/07/2015 03:38 AM, Florent Gallaire wrote: >> I answer to myself : >> db:open('factbook')//city/@name/string() >> will works fine. >> >> Still, is there a best way ? Is there some problems with that ? Is the >> element way better for xquery web usage ? >> >> Cheers >> >> Florent >> >> On Thu, May 7, 2015 at 2:18 AM, Florent Gallaire <fgallaire at gmail.com> wrote: >> > In the RESTXQ documentation there is this example which works fine : >> > >> > declare >> > %rest:path("cities") >> > %output:method("json") >> > %output:json("format=jsonml") >> > function page:cities() >> > { >> > element cities { >> > db:open('factbook')//city/name >> > } >> > }; >> > >> > If I have the same factbook db but with name as attribute and not as >> > element, with the only change in the code the xquery request >> > db:open('factbook')//city/@name I will have the error message: >> > [XQDY0025] Duplicate attribute 'name'. >> > >> > That's ok, but what is the right way in xquery to easily use multiple >> > attributes as return value of request. >> > >> > Is the right way to prefer elements to attributes ? >> > >> > Cheers >> > >> > Florent >> > >> > >> > >> > -- >> > FLOSS Engineer & Lawyer >> >> >> > > -- > Dirk Kirsten, BaseX GmbH, http://basexgmbh.de > |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz > |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: > | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle > `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22 >
--
FLOSS Engineer & Lawyer
Written for BaseX 8.1. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery