【发布时间】:2014-09-19 09:33:52
【问题描述】:
我正在学习 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>
其实
- 我想摆脱包装 div。
- 而不是新的 每个子结果的段落(p ... /p)我只想有一个 每个结果后的换行符 (br)。
所以,预期的结果是这样的:
<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 意外令牌:{ [at 第 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 意外令牌:{ [at 第 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)
【问题讨论】:
-
请始终描述您尝试过的什么以及失败的原因,以及预期的输出究竟是什么。并查看如何正确格式化您的问题(在FAQ),使其更易于阅读(尤其是:代码格式化和枚举)。
-
更正了枚举。但是代码格式有什么问题?
-
问题现在看起来很棒。最好始终使用代码周围的反引号来应用内联代码格式,尤其是在任何可能被解释为 HTML 或 markdown 的情况下。我刚刚添加了一些。