r/xquery • • 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
3 Upvotes

5 comments sorted by

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 //INHABITANT
where $inhabitant/@Gender = "M"
return element MALE {$inhabitant/text() }
}

In brief, we first create the root element using a computed element constructor MALES and then write a FLWOR expression (using only for, where, and return clauses). 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 a MALE element,

1

u/SnooChocolates3391 Feb 20 '22

Hello, thanks for reply, when I wrote this code I got an error: To use Xpath 3.1 syntad, you must ocnfigure the XPath parser to handle it. I have set it to Xquery 3.0. So by the looks of it the code your provided is using xpath, im guessing its because of element function.

2

u/andersoncliffb Feb 20 '22

When you use a FLWOR expression, you are writing XQuery. However, XQuery also contains XPath. For details on the computed element constructor in XQuery 3.0, see the spec. But you could just as easily, and perhaps more appropriately, use a direct element constructor. I'm not sure why you are seeing this syntax error in your XQuery engine since nothing in my expression above uses 3.1 syntax.

1

u/SnooChocolates3391 Feb 20 '22

I changed to videlibri.de/cgi-bin/xidelcgi and now its working almost perfectly, I just need it to make it look like this: <MALES> <MALE> FIRST NAME LAST NAME</MALE> .... </MALES>

1

u/andersoncliffb Feb 20 '22

That's just a question of formatting. You'll probably need to tweak your editor to get those results.