用于遍历每个子元素

时间:2015-03-03 09:00:56

标签: xquery

使用我的XML

<products>
    <product id="12" type="modell">
        <attribute type="image">nose.jpg</attribute>
        <product id="13" type="kid">
            <attribute type="image">face.jpg</attribute>
        </product>
    </product>
    <product id="22" type="modell">
        ...
    </product>
</products>

和xquery我除了在我的产品ID image下获取类型为12的每个属性。

到目前为止我尝试了什么:

 data(for $image in //product[@id = '12']/*/attribute[@type='IMAGE']
 return $image)

哪个不起作用(没有*我只获得nose.jpg),但我正在寻找返回face.jpgnose.jpg的方法。

任何提示?

1 个答案:

答案 0 :(得分:1)

使用descendant-or-self::attribute轴步骤,缩写为//attribute,在整个子树中匹配(仅/*/attribute“跳过”一级):

data(for $image in //product[@id = '12']//attribute[@type='IMAGE']
return $image)

(我需要将IMAGE更改为小写以匹配您的输入,但