XQuery返回错误..?

Joh*_*ohn 2 xml xpath xquery basex

以下是XML文件-

<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>
Run Code Online (Sandbox Code Playgroud)

使用baseX,我正在编写查询以显示Name包含单词Capital的城市,但返回错误。查询是-

/Continents/Continent[contains(Country/City/Desc,'Capital')]/Country/City/Name
Run Code Online (Sandbox Code Playgroud)

并且错误是- 错误:[XPTY0004]预期存在单个项目,(元素Desc {...},元素Desc {...},...)。

请帮帮我。.是否需要对此类查询使用FLWOR?

Chr*_*rün 5

您原来的查询可能已被重写如下...

/Continents/Continent[
  some $x in Country/City/Desc satisfies contains($x,'Capital')]
  /Country/City/Name
Run Code Online (Sandbox Code Playgroud)

...要么...

/Continents/Continent[
  for $x in Country/City/Desc
  where contains($x,'Capital')
  return $x]/Country/City/Name
Run Code Online (Sandbox Code Playgroud)

...但是这两个查询都将返回任何包含城市描述(包含“首都”)的大洲的所有国家/ 地区。相反,这可能是您要查找的内容:

/Continents/Continent/Country/City[contains(Desc,'Capital')]/Name
Run Code Online (Sandbox Code Playgroud)

您也可以使用包含文本 表达式来忽略关键字中的大小写,变音符号等:

/Continents/Continent/Country/City[Desc contains text 'Capital']/Name
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。