以下是XML文件 -
<Continents>
<Continent name="Asia">
<Country name="Japan">
<City name="Tokyo"><Desc>Tokoyo is a most populated City</Desc></City>
<City name="Horishima"><Desc>What to say.. Faced the destruction due to Atom Bomb</Desc></City>
</Country>
<Country name="India">
<City name="New Delhi"><Desc>Capital of India</Desc></City>
<City name="Mumbai"><Desc>Financial Capital of India</Desc></City>
<City name="Lucknow"><Desc>City of Nawabs</Desc></City>
</Country>
</Continent>
</Continents>
我想列出Country = "India"
我的XQuery FLWOR代码是 -
for $x in doc("Continent")/Continents/Continent
where $x/Country/@name='India'
return $x/Country/City/@name
我希望输出为 -
name="New Delhi" name="Mumbai" name="Lucknow"
但输出为 -
name="Tokyo" name="Horishima" name="New Delhi" name="Mumbai" name="Lucknow"
有人可以帮助我获得正确的输出吗?另外如何在单独的线路上获得它?
答案 0 :(得分:2)
您几乎问了同样的问题here,同样的答案也适用于此。你想要的是:
doc("Continent")/Continents/Continent/Country[@name = 'India']/City/@name
如果您希望每个结果都在自己的行上,请尝试以下方法:
string-join(
doc("Continent")/Continents/Continent/Country[@name = 'India']/City/@name,
' '
)
这应该导致:
New Delhi
Mumbai
Lucknow
答案 1 :(得分:1)
你也可以稍微重写你的FLWOR以获得想要的结果:
for $x in doc("Continent")/Continents/Continent/Country
where $x/@name='India'
return $x/City/@name
(请注意,我移动了/ Country部分)
如果有必要,你可以按照Leo的建议将这个FLWOR包装在一个字符串连接中,就像他包装他的XPath表达式一样。
答案 2 :(得分:1)
/*/*/Country[@name='India']/City/concat('name="', @name, '" ')
注意:这也恰好是纯XPath 2.0表达式。