Search code examples
xmlxquerybasex

BaseX XQUERY selecting nodes with the value of a specific attribute


XML sample:


    <mondial>
        <continent id="f0_119" name="Europe"/>
        <continent id="f0_123" name="Asia"/>
        <continent id="f0_126" name="America"/>
        <continent id="f0_129" name="Australia/Oceania"/>
        <continent id="f0_132" name="Africa"/>
        <country id="f0_136" name="Albania" capital="f0_1461" population="3249136"
                 datacode="AL" total_area="28750" population_growth="1.34"
                 infant_mortality="49.2" gdp_agri="55" gdp_total="4100"
                 inflation="16" indep_date="28 11 1912"
                 government="emerging democracy" car_code="AL">
            <name>Albania</name>
            <city id="f0_1461" country="f0_136" longitude="10.7" latitude="46.2">
                <name>Tirane</name>
                <population year="87">192000</population>
            </city>
            <city id="f0_36498" country="f0_136" longitude="19.2" latitude="42.2">
                <name>Shkoder</name>
                <population year="87">62000</population>
                <located_at type="lake" water="f0_39058"/>
            </city>
        </country>
    </mondial>

for $x in //mondial/country/population
where $x/@year=87
return $x/name

I'm basically trying to return <name> from the node <population> that has the attribute of year=87. I know it sounds confusing as heck, I can't even find documentation for this I've tried googling how to select using the attribute but I can't seem to return anything.


Solution

  • Please try the following XQuery.

    XQuery

    for $x in //mondial/country/city
    where $x/population/@year=87
    return $x/name