Xquery错误:属性必须遵循根元素



新年快乐!

我正在学习Xquery,并现在面对以下问题。

我正在解析是分发的一部分的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>标签中,我会看到一个错误消息"属性必须遵循root Element"。

 <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>

编写{$ country/@supers}时,您不会插入群体属性的文本,而是属性本身。如果您没有"以前的乡村人口文本",则使用{$ country/@supers}会创建类似的东西。

如果要其值,请使用:

{data($country/@population)}

{data($pop_c)}

由于您已经将其放在变量中。(也可以使用数字或字符串函数代替数据,但我认为数据是最快的)

最新更新