简体   繁体   English

BaseX XQUERY 选择具有特定属性值的节点

[英]BaseX XQUERY selecting nodes with the value of a specific attribute

XML sample: XML 样品:


    <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 .我基本上是在尝试从具有year=87属性的节点<population>返回<name> 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.我知道这听起来很令人困惑,我什至找不到这方面的文档我试过用谷歌搜索如何使用该属性搜索 select 但我似乎无法返回任何内容。

Please try the following XQuery.请尝试以下 XQuery。

XQuery XQuery

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

Please note that the population elements in your XML document are children of the city elements.请注意,XML 文档中的population元素是city元素的子元素。 Next, you tried to loop over the population elements, and it's better to move this path step into the where clause.接下来,您尝试循环遍历population元素,最好将此路径步骤移到where子句中。 One solution for returning the city names is as follows:返回城市名称的一种解决方案如下:

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

You can get to the required information with a single XPath您可以通过单个 XPath 获取所需信息

//city[population/@year=87]/name

Live Demo (xqueryfiddle)现场演示 (xqueryfiddle)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM