For循环遍历每个子元素

For loop over every child elements

和我的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 我除了在我的产品下获取每个类型为 image 的属性,id 为 12.

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

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

这是行不通的(没有 * 我只得到 nose.jpg),但我正在寻找 return face.jpgnose.jpg.

有什么提示吗?

使用 descendant-or-self::attribute 轴步骤,缩写为 //attribute,它在整个子树中匹配(而 /*/attribute 仅 "skips" 一级):

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

(不过我需要将 IMAGE 更改为小写以匹配您的输入)