[英]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.