XQuery with Nested Return
Hello I was wondering if someone could help me with this awfully frustrating conundrum. What I am trying to do is have the return return both a single value and the results of another xquery. The tricky part is that I do not want to return the content as XML but rather in plain text.*
My specific case is that I want to find every enumeration in an XSD, print out the name of the datatype, and underneath it the list of all its possible values.
Now this works:
Code:
declare namespace xsd = "http://www.w3.org/2001/XMLSchema";
declare variable $url := ".....";
declare variable $src := doc($url);
declare variable $nl := codepoints-to-string(10);
for $type in $src//xsd:simpleType[.//xsd:enumeration]
let $typeName := string($type/@name)
return <x>{concat($nl,"===",$typeName,"===")}{
for $value in $type/xsd:restriction/xsd:enumeration/@value
let $enumName := string($value)
return concat($nl,"* ",$enumName)
}</x>
But if I try to remove the <x></x> it fails. I've tried every possible combination of {} and , imaginable. :mad: I also tried passing the second XQuery as an argument to concat() but that didn't work either. While I could do some post processing but since I'd like to be able to rely on XQuery in the future I want how to learn to do this the proper way.
I tried Googling but all examples show only the XML version and I'm certain there must be a way to do this for plain text
* Technically I'm trying to output it in MediaWiki syntax.
Re: XQuery with Nested Return
The result of an XQuery is always an XML node. You will need to get your XML out and parse it to get the values you want in your 'client' language that deals with it - javascript, C#, PHP, whatever.
So the proper way is to get your node out and then parse it. XQuery isn't meant to be an XML processing/extraction language - think of it like SQL. You query rowsets in tables and you get rowsets out (even a single value is a rowset of one row, one column).
So you can do what you do currently
Code:
return <enumerations>{concat($nl,"===",$typeName,"===")}{
for $value in $type/xsd:restriction/xsd:enumeration/@value
let $enumName := string($value)
return concat($nl,"* ",$enumName)
}</enumerations>
Or you could do
Code:
<enumerations>
{
for $type in $src//xsd:simpleType[.//xsd:enumeration]
let $typeName := string($type/@name)
return {concat($nl,"===",$typeName,"===")}{
for $value in $type/xsd:restriction/xsd:enumeration/@value
let $enumName := string($value)
return concat($nl,"* ",$enumName)
}
}
</enumerations>
Not sure about the second example, I've typed it from memory of XQuery.
Re: XQuery with Nested Return
Out of curiosity, what XQuery parser/server do you use?
Re: XQuery with Nested Return
QExo
it was a small download and was easy to set up... and I am a Java developer.
Would you recommend something else?
Re: XQuery with Nested Return
Hmm, just an idea, but would there be a way to return the result in the XML and then some how call on it? Don't have QExo infront of me right now or else I'd try it out; will do as soon as I get the chance
Re: XQuery with Nested Return
No. Calling a 'text' method on it won't make sense. It is a list of nodes that you're getting back, not a single node. You will need to have a language that receives those nodes and extracts values out of it.
Re: XQuery with Nested Return
Quote:
Originally Posted by CaptainPinko
QExo
it was a small download and was easy to set up... and I am a Java developer.
Would you recommend something else?
In my experience, they've all been terrible. Each one worse than all the others, which is a statistical impossibility.