作为 HTTP POST 请求中的 XML 内容,我收到了在 Xquery 3.1 (eXist-db 5.2) 中处理的以下内容:
<request id="foo">
<p>The is a description with a line break<br/>and another linebreak<br/>and
here is an ampersand&.</p>
<request>
我的目标是获取节点<p>
并将其插入到 eXist-db 中的 TEI 文件中。如果我只是按原样插入片段,则不会引发任何错误。
但是我需要转换字符串 <br/>
的任何实例进入元素<lb/>
在将其添加到 TEI 文档之前。我尝试使用 fn:parse-xml
.
但是,应用以下内容会在 &
上引发错误...这让我感到惊讶:
let $xml := <request id="foo">
<p>The is a description with a line break<br/>and
another linebreak<br/>and here is an ampersand&.</p>
<request>
let $newxml := <p>{replace($xml//p/text(),"<br/>","<lb/>")}</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.
如果我删除 &
该片段解析得很好。如果它是合法的 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<br/>and
another linebreak<br/>and here is an ampersand&.</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/