Marklogic - 如何删除 XQuery 中的新行字符问题
Marklogic - How to remove new line char issue in XQuery
如何去除换行符。
Sample Code.
let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return replace($b//title/text(),'\n','')
Expected Output :
Fees • Levies Other file
我能想到的几个选项:
fn:replace() - 但您必须考虑这两个可能的字符
let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return fn:replace(fn:data($b//title), '(\r?\n|\r)', '')
但这可能会给您留下额外的空格,具体取决于它的创建和格式化方式,因此,您可能还更喜欢使用 fn:normalize-space()
立即清理空格和换行符
let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return fn:normalize-space(fn:data($b//title))
您可能还注意到我使用了 fn:data()。这是出于习惯。但是,有一篇关于文本、字符串、数据的精彩博文 here。
如何去除换行符。
Sample Code.
let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return replace($b//title/text(),'\n','')
Expected Output :
Fees • Levies Other file
我能想到的几个选项:
fn:replace() - 但您必须考虑这两个可能的字符
let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return fn:replace(fn:data($b//title), '(\r?\n|\r)', '')
但这可能会给您留下额外的空格,具体取决于它的创建和格式化方式,因此,您可能还更喜欢使用 fn:normalize-space()
立即清理空格和换行符let $b := <root><title>Fees • Levies<?brk type="line"?>
Other file</title></root>
return fn:normalize-space(fn:data($b//title))
您可能还注意到我使用了 fn:data()。这是出于习惯。但是,有一篇关于文本、字符串、数据的精彩博文 here。