XQuery 错误 : Attribute must follow the root element

标签 xquery basex

祝大家新年快乐!

我正在学习 XQuery BaseX现在面临以下问题。

我正在解析 factbook.xml文件是发行版的一部分。

以下查询运行正常:

 for $country in db:open('factbook')//country
 where $country/@population < 1000000 and $country/@population > 500000
 return <country name="{$country/name}" population="{$country/@population}">
 {
   for $city in $country/city
   let $pop := number($city/population)
   order by $pop descending
    return <city  population="{$city/population/text()}"> {$city/name/text()}
    </city>
 }
 </country>

但是在尝试生成运行第二个查询的 html 时 - 如果我尝试将 "{$country/@population}"<h2>Country population: </h2>我看到一条错误消息“属性必须遵循根元素”。

 <html><head><title>Some Countries</title></head><body>
 {
  for $country in db:open('factbook')//country
 let $pop_c := $country/@population
 where $pop_c < 1000000 and $pop_c > 500000
 return
 <p>
 <h1>Country: {$country/name/text()}</h1>
 <h2>Country population: #error comes if I put it here!#</h2>
 {
 for $city in $country/city
 let $pop := number($city/population)
 order by $pop descending
 return ( <h3>City: {$city/name/text()}</h3>,
 <p>City population: {$city/population/text()}</p>
 )
 }
 </p>
 }
 </body></html>

我的错误在哪里? 谢谢!

最佳答案

仅使用:

{$country/@population}

复制结果中的属性总体。属性应该紧跟在元素(或元素后面的其他属性)之后——但是这个属性紧跟在文本节点之后,这会导致引发错误。

使用:

<h2>Country population: {string($country/@population)} </h2>

关于XQuery 错误 : Attribute must follow the root element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110279/

相关文章:

date - 获取 Marklogic 中选定日期的第一天和最后一天日期

xquery - 使用 mlcp、csv 文件和转换解析日期导入数据

xslt - XQuery 链接 XSLT 转换

vb.net - xquery 上限函数的奇怪结果

java - XQuery 对命名空间声明的查询意外结束

xpath - XQuery:按特定顺序查找具有后代的节点

字符类中的正则表达式 anchor

xml - 使用 BaseX 读取多个 XML 文件

XML:如何在整个 xml 中搜索并通过 id 查找节点并删除它们?

java - 我如何从java中的xquery获取json?