xpath - Xquery 函数 parse-xml() 在 &? 上产生错误

标签 xpath xquery exist-db

作为 HTTP POST 请求中的 XML 内容,我收到了在 Xquery 3.1 (eXist-db 5.2) 中处理的以下内容:

<request id="foo">
     <p>The is a description with a line break&lt;br/&gt;and another linebreak&lt;br/&gt;and
            here is an ampersand&amp;.</p>
<request>

我的目标是获取节点<p>并将其插入到 eXist-db 中的 TEI 文件中。如果我只是按原样插入片段,则不会引发任何错误。

但是我需要转换字符串 &lt;br/&gt; 的任何实例进入元素<lb/>在将其添加到 TEI 文档之前。我尝试使用 fn:parse-xml .

但是,应用以下内容会在 &amp 上引发错误...这让我感到惊讶:

let $xml := <request id="foo">
                 <p>The is a description with a line break&lt;br/&gt;and 
                    another linebreak&lt;br/&gt;and here is an ampersand&amp;.</p>
           <request>
let $newxml := <p>{replace($xml//p/text(),"&lt;br/&gt;","&lt;lb/&gt;")}</p>
return <p>{fn:parse-xml($newxml)}</p>

错误:

Description: err:FODC0006 String passed to fn:parse-xml is not a well-formed XML document.: Document is not valid.
Fatal : The entity name must immediately follow the '&' in the entity reference.

如果我删除 &amp;该片段解析得很好。如果它是合法的 XML,为什么会产生错误?我怎样才能达到所需的结果?

提前非常感谢。

ps。我对 Xquery 和 XSLT 解决方案持开放态度。

最佳答案

问题似乎出在 HTML 实体上。它将适用于数字实体(即 < 而不是 <> 而不是 >),但 XML 解析器不知道 HTML 字符实体。

使用util:parse-html()而不是 fn:parse-xml()

let $xml := <request id="foo">
                  <p>The is a description with a line break&lt;br/&gt;and 
                    another linebreak&lt;br/&gt;and here is an ampersand&amp;.</p>
           </request>
return <p>{util:parse-html($xml/p/text())/HTML/BODY/node()}</p>

关于xpath - Xquery 函数 parse-xml() 在 &? 上产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62755987/

相关文章:

java - 使用 xpath 定位按钮 - Chrome 中的 JAVA/Selenium 2

python - 正确的 xpath 来卷起子项的文本

xml - 通过 Xquery 将 XML 中的数据转换为文本

perl - BaseX : how to set up 中的基准测试

xml - XQuery HTML 数组输入

xml - 如何使用 eXist-db 将 "stitch together"结果转换为单个 XML 文档?

javascript - 将 Javascript/jQuery 查询转换为 XPath

XML 上的 SQL 迭代

SQL - 空白默认命名空间

xml - 查询 : how to get the previous element than the one chosen in an XML file?