XQuery fn:replace not behaving as expected

微笑、不失礼 提交于 2019-12-24 20:12:44

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2901441/xquery-fnreplace-not-behaving-as-expected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!