XQuery 返回错误..? [英] XQuery returning an error..?
问题描述
下面是 XML 文件 -
<Continent n="亚洲"><国家 n="泰国"><城市><姓名>曼谷</姓名><泰国首都<</城市></国家><国家 n="印度"><城市><姓名>新德里</姓名><资本在印度</城市><城市><姓名>孟买</姓名><印度的金融资本</城市><城市><姓名>钦奈</姓名><一个非常好的城市<Desc></城市></国家></大陆></大陆>
使用 baseX,我正在编写查询以显示包含单词 Capital 的城市的 Name
,但返回错误.查询是 -
/Continents/Continent[contains(Country/City/Desc,'Capital')]/Country/City/Name
错误是 -错误:[XPTY0004] 应为单个项目,(元素 Desc { ... },元素 Desc { ... }, ...)找到.
请帮帮我.. 此类查询是否需要使用 FLWOR?
您的原始查询可以改写如下...
/大陆/大陆[Country/City/Desc 中的一些 $x 满足 contains($x,'Capital')]/国家/城市/名称
...或...
/大陆/大陆[国家/城市/描述中的 $x其中包含($ x,'资本')返回 $x]/国家/城市/名称
...但两个查询都将返回任何大陆的所有国家,其中包含包含首都"的城市描述.相反,这可能是您正在寻找的:
/Continents/Continent/Country/City[contains(Desc,'Capital')]/Name
您也可以使用 contains text 表达式来忽略大小写、变音符号等. 用你的关键词:
/Continents/Continent/Country/City[Desc contains text 'Capital']/Name
希望这会有所帮助.
Below is the XML file -
<Continents>
<Continent n="Asia">
<Country n="Thailand">
<City>
<Name>Bangkok</Name>
<Desc>Capital on Thailand</Desc>
</City>
</Country>
<Country n="India">
<City>
<Name>New Delhi</Name>
<Desc>Capital on India</Desc>
</City>
<City>
<Name>Mumbai</Name>
<Desc>Financial capital on India</Desc>
</City>
<City>
<Name>Chennai</Name>
<Desc>A very good city</Desc>
</City>
</Country>
</Continent>
</Continents>
Using baseX, I am writing a query to display the Name
of cities containing the word Capital but is returning error. The query is -
/Continents/Continent[contains(Country/City/Desc,'Capital')]/Country/City/Name
and the error is - Error: [XPTY0004] Single item expected, (element Desc { ... }, element Desc { ... }, ...) found.
Please help me out.. Is it required to use FLWOR for such queries?
Your original query could have been rewritten as follows...
/Continents/Continent[
some $x in Country/City/Desc satisfies contains($x,'Capital')]
/Country/City/Name
...or...
/Continents/Continent[
for $x in Country/City/Desc
where contains($x,'Capital')
return $x]/Country/City/Name
...but both of the queries will return all countries of any continent that contains a city description containing 'Capital'. Instead, this is probably what you are looking for:
/Continents/Continent/Country/City[contains(Desc,'Capital')]/Name
You may as well use the contains text expression to ignore case, diacritics, etc. in your key words:
/Continents/Continent/Country/City[Desc contains text 'Capital']/Name
Hope this helps.
这篇关于XQuery 返回错误..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!