XQuery fn:替换不符合预期的行为 [英] XQuery fn:replace not behaving as expected
问题描述
我有一个XML格式的Excel工作表,其中包含
<单元格ss:StyleID =s127>< ;数据ss:Type =String>替换我< / Data>< / Cell>
我想用不同的字符串替换@ A01-Replace。我正在使用XQuery的替换函数,如下所示:
let $ excel:= doc(document.xml)
pre>
let $ test:=另一个字符串
返回替换($ excel,替换我,$ test)
在调用replace之前,变量$ excel在输出时是有效的XML。但是,当我调用replace函数之后输出$ excel时,所有的XML标签都被删除,而$ excel是一个字符串,其中单元格的内容是它的值。我想保留XML标签。
我期待的是
<单元格ss:StyleID = s127>< Data ss:Type =String>另一个字符串< / Data>< / Cell>
但是,我得到
另一个字符串
所有的XML标签都被删除。
任何想法?
解决方案
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
basednode-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.这篇关于XQuery fn:替换不符合预期的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!