XQuery fn:replace not behaving as expected
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 开发者_StackOverflow中文版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.
精彩评论