我正在学习XQuery并尝试了一个中等复杂的示例。该查询本身可以工作,但是我无法按照我想要的方式添加HTML。
以下示例有效(在eXist-db XQuery引擎中)
for $current_value in $another_variable//some_tag/@attribute
return
<div><h1>{$current_value}</h1>
{
for $current_sub_value in $another_variable//some_tag
where $current_sub_value/@attribute = $current_value
return <p> { data($current_sub_value/@another_attribute) } </p>
}
</div>
但实际上
因此,预期结果将是这样的:
<h1> ... some text here ... </h1>
some text here ... <br />
another line here ... <br />
但是,无论我尝试过什么,我总是会遇到语法错误。
似乎可以使用封装XML标记中声明XQuery { ... }
。但是,如果XML标记不在XQuery周围,而是在XQuery之前或之后,该怎么办?
有没有办法告诉XQuery引擎:这是XQuery的一部分,这里是一些HTML,只需将它们连接在一起即可?(XPath函数concat()
对我不起作用,它导致<br />
被显示,< >
并且确实可以逃脱。)
如上所述,我已经尝试了一些语法,但是总是收到错误消息。这是我所做的:
测试1/3
for $current_value in $another_variable//some_tag/@attribute
return
<h1>{$current_value}</h1>
{
for $current_sub_value in $another_variable//some_tag
where $current_sub_value/@attribute = $current_value
return <p> { data($current_sub_value/@another_attribute) } </p>
}
结果:
执行表达式时发现错误:org.exist.xquery.XPathException:err:XPST0003意外令牌:{[在第4行,第5列]
测试2/3
for $current_value in $another_variable//some_tag/@attribute
{
return
<h1>{$current_value}</h1>
for $current_sub_value in $another_variable//some_tag
where $current_sub_value/@attribute = $current_value
return <p> { data($current_sub_value/@another_attribute) } </p>
}
结果:
执行表达式时发现错误:org.exist.xquery.XPathException:err:XPST0003意外令牌:{[在第2行,第1列]
测试3/3
for $current_value in $another_variable//some_tag/@attribute
return
<div>
<h1>{$current_value}</h1>
{
for $current_sub_value in $another_variable//some_tag
where $current_sub_value/@attribute = $current_value
return data($current_sub_value/@another_attribute) <br/>
}
结果:
意外的标记:(
>
同时期望元素构造函数的结束标记:div)
您基本上想要的是一系列项目。在XML中,元素始终只有一个根节点。因此,例如,关于您的段落,<p>something</p>
是一个元素,而所需的输出something<br/>
是一系列项目,在这种情况下是字符串和元素。
在XQuery中,序列只是用括号括起来,例如('s', <a/>, 45)
是一个序列。
因此,在您的情况下,以下应该可以正常工作:
for $current_value in $another_variable//some_tag/@attribute
return (
<h1>{$current_value}</h1>,
for $current_sub_value in $another_variable//some_tag
where $current_sub_value/@attribute = $current_value
return (data($current_sub_value/@another_attribute), <br />)
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句