Click to See Complete Forum and Search --> : New to XQuery, help needed


2003 UB 313
05-16-2010, 10:24 AM
1) I have to submit some XML queries for my college work.

How to return multiple rows in XQuery?

for $x in doc("India.xml")/India/state
where $x/language="Hindi"
return
<state> $x/state_name </state>,
<language> $x/language </language>

returns
<state> $x/state_name </state>
<state> $x/state_name </state>
<state> $x/state_name </state>
<state> $x/state_name </state>
<state> $x/state_name </state>
<state> $x/state_name </state>
<state> $x/state_name </state>
<language> $x/language </language>

How to make it return the proper names of states?

return
<state> {$x/state_name} </state>,
<language> {$x/language} </language>
says that $x has not been declared

return {
<state> {$x/state_name} </state>,
<language> {$x/language </language>}
}

says that Unexpected {

2) How to find maximum of values?
for $x in doc("India.xml")/India/state
return max($x/area)
is rather returning up with something like this

<?xml version="1.0" encoding="UTF-8"?>130058 38863 191791 275068 155707 307690 443446 88752 173877 294411 196024 342239 3702 50862 44212 55673 222236 78438 83743 16579 22327 21081 10486 22429 7096 1483 492 114 112 491 8249 32

sohguanh
05-16-2010, 09:25 PM
1) I have to submit some XML queries for my college work.

How to return multiple rows in XQuery?

for $x in doc("India.xml")/India/state
where $x/language="Hindi"
return
<state> $x/state_name </state>,
<language> $x/language </language>


1.
Hi, for XQuery, it would be good to include a small sample input XML. Since you did not provide, I assume below.

<India>
<state>
<language>Hindi</language>
<state_name>State A</state_name>
</state>
<state>
<language>ABC</language>
<state_name>State B</state_name>
</state>
<state>
<language>Hindi</language>
<state_name>State C</state_name>
</state>
</India>


for $x in doc("India.xml")/India/state
where $x/language="Hindi"
return
<state> {$x/state_name} {$x/language} </state>


2.
XQuery has a lot of built-in functions so you may want to search the XQuery implementation you have. If it is not there, you can either build your own max function and called them inside the XQuery or search Internet for Open Source XQ functions.

One place is http://www.xqueryfunctions.com and the author Priscilla Walmsley is heavily involved in XQuery specs and/or implementations. I learn a lot from her books and tutorials. I would welcome newbie to XQuery to take a look at her works.

After you feel you are comfortable with XQuery, you need to find a good Open Source and fast XQuery processor/implementation. I would say give BaseX (which goes beyond just XQuery, they have a XML ground-up database) a second look. My personal opinion.