xpathxqueryzorba

Storing the value of attribute and matching it in Xquery


<a>
{
for $p in doc("x.xml")/user//district
where data($p/@population) div data($p/@area) > 20
     return <b>
                <c> {$p/@name/string()} </c>
                <d> {$p/@population div $p/@area} </d>
                <d> {$p/@city/string()}</e>
            </b>
}
</a>

I am getting the id of the district but I want the name of the country . So i want to traverse back to the district from city. Like in snippet below, I am getting country(id = 'f0_149') but i am unable to match it and print the country name 'Austria'instead.

I am new to Xquery so I am unaware about many things.

XML FILE SNIPPET:

<?xml version="1.0" encoding="UTF-8"?>
    <user xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <country id='f0_149' name='Austria' capital='f0_1467' population='10000000' total_area='50000' >
            <name>
                Austria
            </name>
            <district id='f0_17440' name='Burgenland' country='f0_149' capital='f0_2291' population='250000' area='5000'>
                <city id='f0_2291' country='f0_149' province='f0_17440'>
                    <name>
                        Eisenstadt
                    </name>
                    <population year='87'>
                        10102
                    </population>
                </city>

Solution

  • You can use the parent:: XPath axis:

    <c> { $p/parent::country/@name/string() } </c>