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
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@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
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 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@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
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
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@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
basex-talk@mailman.uni-konstanz.de