返回多元素 HTML 作为 xquery 结果
我在返回多元素 HTML 结果时遇到问题(我不知道这个结果的名称)。为了更清楚地说明这一点,请参阅下面的代码。
<body>
<h1>Nobel prize winners</h1>{
for $subject in distinct-values(doc("nobel.xml")//subject)
let $sn := $subject`
return <div><h2>{$sn}</h2><ul>
{for $row in doc("nobel.xml")/nobel/row
let $name := $row/winner/text()
let $year := $row/yr/text()
where $row/subject/text()=$subject
order by $year
return <li> ({$year} {$name} </li>
}</ul></div>
}
</body>
该代码可以工作,但您可以注意到我必须将
和
的开头和结尾return
部分,这是我实际上不想要的。我的问题是,有什么办法可以去掉吗?我尝试过 { }
和 ( )
,但两者都不起作用。I have a problem with returning multi-element HTML result (I dont know the name for this). To make it clearer, please see the code below.
<body>
<h1>Nobel prize winners</h1>{
for $subject in distinct-values(doc("nobel.xml")//subject)
let $sn := $subject`
return <div><h2>{$sn}</h2><ul>
{for $row in doc("nobel.xml")/nobel/row
let $name := $row/winner/text()
let $year := $row/yr/text()
where $row/subject/text()=$subject
order by $year
return <li> ({$year} {$name} </li>
}</ul></div>
}
</body>
That code will work, but you can notice that I have to put that <div>
and </div>
in the beginning and the end of the return
part, which I actually don't want. My question is, is there any way to remove it? I have tried with { }
and ( )
and both don't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是 XQuery 1.0 语法的语法规则,取自 XQuery 1.0 w3c 规范:
可以清楚地看到,
return
标记(关键字)后面必须跟有单个表达式。但是,通过删除
div
元素,您拥有:正如我们可以清楚地看到的,
return
关键字后面没有跟随单个表达式,由 XQuery 1.0 语法定义。解决方案:明确指定
return
关键字后面的元素序列,作为序列:注意,
return后面的元素序列
现在被括号包围,并且序列中的元素由逗号分隔。现在,当上面的 XQuery 代码在此
nobel.xml
文件上执行时:生成所需的正确结果:
Here are the syntax rule for the grammar of XQuery 1.0 , as taken from the XQuery 1.0 w3c spec.:
As can be clearly seen, the
return
token (keyword) must be followed by a single expression.However, by getting rid of the
div
element you have:As we can clearly see, the
return
keyword is not followed by a single expression, as defined by the XQuery 1.0 grammar.Solution: Clearly specify the sequence of elements that follow the
return
keyword, as a sequence:Notice that the sequence of elements following
return
is now surrounded by brackets, and that the elements in the sequence are delimited by a comma.Now, when the above XQuery code is executed on this
nobel.xml
file:the wanted, correct result is produced: