r/xquery • u/SnooChocolates3391 • Feb 20 '22
XQUERY FLWOR using internal document, how?
Im using http://xpatheval.apphb.com/ for this, so XML and XQUERY is basically in the same file.
And since I need to use FLWOR I don't know how I should approach the part where I need to locate doc() and it cant be empty
My XML:
<?xml version="1.0"?> <RIGA> <STREET Name="Brivibas"> <HOUSE No="1"> <FLAT No="1"> <INHABITANT Year="1970" Gender="M">Ivanovs Sergejs</INHABITANT> <INHABITANT Year="1970" Gender="F">Ivanova Anna</INHABITANT> </FLAT> <FLAT No="2"> <INHABITANT Year="1972" Gender="M">Strods Janis</INHABITANT> <INHABITANT Year="1972" Gender="F">Strode Inna</INHABITANT> <INHABITANT Year="1974" Gender="F">Strode Julija</INHABITANT> </FLAT> </HOUSE> <HOUSE No="2"> <FLAT No="1"> <INHABITANT Year="1974" Gender="M">Zemzars Uldis</INHABITANT> </FLAT> </HOUSE> </STREET> <STREET Name="Elizabetes"> <HOUSE No="1"> <FLAT No="1"> <INHABITANT Year="1985" Gender="M">Celms Juris</INHABITANT> </FLAT> <FLAT No="2"> <INHABITANT Year="1988" Gender="M">Dmitrijevs Igors</INHABITANT> <INHABITANT Year="1988" Gender="F">Dmitrijeva Svetlana</INHABITANT> <INHABITANT Year="1989" Gender="F">Dmitrijeva Olga</INHABITANT> <INHABITANT Year="1989" Gender="M">Dmitrijevs Pjotrs</INHABITANT> </FLAT> </HOUSE> </STREET> </RIGA>
I haven't written anything in XQUERY since I dont know how to grab information but my Goal is to have this output using FLWOR system (for, where, order by, return):
<MALES> <MALE>First name Last name</MALE> <MALE>First name Last name</MALE> <MALE>First name Last name</MALE> <MALE>First name Last name</MALE> <MALE>First name Last name</MALE> <MALE>First name Last name</MALE></MALES
2
u/andersoncliffb Feb 20 '22 edited Feb 20 '22
As far as I can see, you do not need to use the
doc()function since the XML you provide on the right is the implicit context for the XPath/XQuery on the right. Also make sure to switch the engine from XPath to XQuery when seeking to write a FLWOR expression. As for the expression itself, there are a couple ways to write it, but here is one example:'element MALES {for $inhabitant in //INHABITANTwhere $inhabitant/@Gender = "M"return element MALE {$inhabitant/text() }}In brief, we first create the root element using a computed element constructor
MALESand then write a FLWOR expression (using onlyfor,where, andreturnclauses). This collects all the names of the inhabitants and then filters out anyone who is not male. We then use another computed element constructor to wrap the text of the name in aMALEelement,