gpt4 book ai didi

html - XQuery 和空的 HTML 标签

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:34 25 4
gpt4 key购买 nike

我正在学习 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>
}

结果:

error found while executing expression: org.exist.xquery.XPathException: err:XPST0003 unexpected token: { [at line 4, column 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>
}

结果:

error found while executing expression: org.exist.xquery.XPathException: err:XPST0003 unexpected token: { [at line 2, column 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/>
}

结果:

unexpected token: > (while expecting closing tag for element constructor: 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 />)
)

关于html - XQuery 和空的 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24991142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com