[英]change value of element by xquery
<category>
<catid>1</catid>
<cattext> sport </cattext>
</category>
我想使用 xquery 将元素<cattext>
文本更改为另一个类似“艺术”而不是运动的文本
如果您的引擎支持更新和脚本:
declare variable $x :=
<category>
<catid>1</catid>
<cattext> sport </cattext>
</category>;
replace value of node $x/cattext with "art";
$x;
或者,如果您不想保留更改,则可以转换它的副本:
let $x :=
<category>
<catid>1</catid>
<cattext> sport </cattext>
</category>
return
copy $changedx := $x
modify (replace value of node $changedx/cattext with "art")
return $changedx
这些代码片段在http://try.zorba.io/上成功运行
如果您的 XQuery 处理器不支持更新,Alejandro 的解决方案是最重要的。
declare namespace local = "http://example.org";
declare function local:copy-replace($element as element()) {
if ($element/self::cattext)
then <cattext>art</cattext>
else element {node-name($element)}
{$element/@*,
for $child in $element/node()
return if ($child instance of element())
then local:copy-replace($child)
else $child
}
};
local:copy-replace(/*)
输出:
<?xml version="1.0" encoding="UTF-8"?>
<category>
<catid>1</catid>
<cattext>art</cattext>
</category>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.