这是我的新手,很容易陷入SQL模式。 我准备了一个小例子来说明我的问题。
<bigXML>
<Product>
<Attributes>
<Attribute>
<Descriptions>
<Description languageCode='DE'>Farbe</Description>
<Description languageCode='EN'>Color</Description>
</Descriptions>
<Value>0000ff</Value>
</Attribute>
<Attribute>
<Descriptions>
<Description languageCode='DE'>Länge</Description>
<Description languageCode='EN'>Length</Description>
</Descriptions>
<Value>2 mm</Value>
</Attribute>
<Attribute>
<Descriptions>
<Description languageCode='DE'>Name</Description>
<Description languageCode='EN'>Name</Description>
</Descriptions>
<Value>Circle</Value>
</Attribute>
</Attributes>
</Product>
</bigXML>
我想达到 0000ff 的值。 这是我的尝试:
<Reply>{for $i in //Product where $i/Attributes/Attribute/Descriptions/Description="Color" return $i/Attributes/Attribute/Value}</Reply>
它返回所有VALUE标签的值,尽管我专门(也许是?)询问了描述为颜色的标签。
请告诉我我在WHERE语法的哪一部分出错了。
答案 0 :(得分:3)
由于您只修复了Product
循环中的for
节点,然后向下遍历了Attribute
两次(一次检查一次,一次检索Value
),则无法确保仅获取Color
属性的值。您的查询说:“对于每个具有一个或多个Product
属性的Color
,返回所有属性值。”
一个简单的解决方法是仅遍历属性而不是产品:
<Reply>{
for $attr in //Product/Attributes/Attribute
where $attr/Descriptions/Description="Color"
return $attr/Value
}</Reply>
如果您还需要产品参考,则可以使用嵌套循环:
<Reply>{
for $i in //Product
for $attr in $i/Attributes/Attribute
where $attr/Descriptions/Description="Color"
return $attr/Value
}</Reply>
您还可以用 XPath 谓词替换where
,并使整个表达式更短:
<Reply>{
//Product/Attributes/Attribute[Descriptions/Description="Color"]/Value
}</Reply>
答案 1 :(得分:0)
尝试
//Attribute[Descriptions[Description='Color']]/Value
它选择0000ff
。