且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

XQuery fn:替换不符合预期的行为

更新时间:2022-11-20 11:36:16

fn:replace 是一个来自XQuery的简单字符串操作函数。



此函数适用于String而不是 node()* code> data-type 。

  let $ replaced-str:= fn:replace ($ excel / Data / text(),替换我,$ test)

应该返回另一个字符串。



如果要使用XML文档,则需要使用
xdmp:node-replace 类型函数,它特定于MarkLogic XmlDatabase。



如果你想要一个简单的 XQuery / Xml / Memory 基于 node-replace ,那么您可以编写一个简单的递归替换方法,该方法接受XPath并重新创建一个新节点。或
查看此MarkLogic-commons示例


I have an Excel worksheet in XML format which contains

<Cell ss:StyleID="s127"><Data ss:Type="String">Replace Me</Data></Cell>

I want to replace @A01-Replace with a different string. I'm using the XQuery's replace function like so:

let $excel := doc("document.xml")

let $test := "another string"

return replace($excel, "Replace Me", $test)

Before calling replace, the variable $excel is valid XML upon output. However, when I output $excel after I call the replace function, all of the XML tags have been stripped, and $excel is a string with the content of the cells as its values. I would like to keep the XML tags there.

What I expect is

<Cell ss:StyleID="s127"><Data ss:Type="String">another string</Data></Cell>

However, I get

another string

All the XML tags are stripped out.

Any ideas?

fn:replace is a simple String Manipulation Function from XQuery.

This function works with a String and not node()* types data-type.

let $replaced-str := fn:replace( $excel/Data/text(), "Replace Me", $test)

should return another string.

If you want to work with an XML document then you need to use xdmp:node-replace type function which is specific to MarkLogic XmlDatabase.

If you want a simple XQuery/Xml/Memory based node-replace then you can write a simple recursive replace method which accepts an XPath and re-creates a new node. Or check out this MarkLogic-commons example.