通常应避免使用 in-mem-update 库,特别是在对同一文档进行多次方法调用时。由于每个方法都会遍历整个节点树并生成一个全新的文档,因此如果您针对大型文档运行和/或对这些文档进行一堆 mem:* 方法调用,则可能会很慢且昂贵。
Ryan Dew 是更好的替代品XQuery XML 内存操作库 https://github.com/ryanjdew/XQuery-XML-Memory-Operations,或使用 XSLT。
下面是如何使用 XSLT 执行这种“合并”的示例,它的性能应该比 in-mem-update 方法更好:
declare variable $STAGING := document{
<root>
<ID>1</ID>
<value1>India</value1>
<value2>USA</value2>
<value3>Russia</value3>
<value4>Srilanka</value4>
<value5>Europe</value5>
<value6>Antartica</value6>
<value7>Spain</value7>
</root>
};
declare variable $FINAL := document{
<root>
<ID>1</ID>
<value1></value1>
<value2></value2>
<value3></value3>
<value4></value4>
<value5>Europe</value5>
<value6>Antartica</value6>
</root>
};
declare variable $XSLT :=
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:param name="staging-doc"/>
<xsl:param name="element-names"/>
<xsl:variable name="final-doc" select="/"/>
<xsl:key name="staging-elements" match="root/*[local-name() = $element-names]" use="local-name()"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/*[local-name() = $element-names]">
<!--if there is an element in the staging-elements doc with this name, use it. Otherwise, use the matched element from this doc -->
<xsl:copy-of select="(key('staging-elements', local-name(.), $staging-doc), .)[1]"/>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<!-- also copy any other elements with the specified names from the staging document that were not already in the final -->
<xsl:apply-templates select="$staging-doc/root/*[local-name() = $element-names and not(key('staging-elements', local-name(), $final-doc))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>;
declare variable $PARAMS := map:new((
map:entry("staging-doc", $STAGING),
map:entry("element-names", tokenize(("value1,value2,value3,value4,value7"), ",") )
));
xdmp:xslt-eval($XSLT, $FINAL, $PARAMS)