我尝试创建删除功能时遇到了问题。我目前的代码是:
的Xquery:
declare variable $d as xs:string;
declare variable $p as xs:string;
let $xp := saxon:evaluate(concat("doc('",$d,"')",$p))
return document {for $n in doc($d)/* return qsx10p8:delete($n, $xp)}
declare function qsx10p8:delete
($n as node(), $xp as node()*)
as node() {
if ($n[self::element()])
then element
{fn:local-name($n)}
{for $c in $n/(*|@*)
return qsx10p8:delete($c, $xp),
if (some $x in $xp satisfies ($n is $x))
then ()
else ($n/text())}
else $n
};
如果输入为:$d = C:/supplier.xml and $p= /Suppliers/Supplier/*
结果是:
<Suppliers><Supplier><address /><Phone /></Supplier></Suppliers>
但我希望结果为<Suppliers><Supplier></Supplier></Suppliers>
。
有没有办法编辑我的功能代码以删除那些必要的标签?
答案 0 :(得分:0)
您可以尝试使用以下递归函数作为删除所需元素的方法。
declare function local:transform ($x as node())
{
typeswitch ($x)
case element(Supplier) return element {"Supplier"} {}
case text() return $x
default
return element { fn:node-name($x) }
{
$x/attribute::*,
for $z in $x/node() return local:transform($z)
}
};
let $d := <Suppliers>
<Supplier><address /><Phone /></Supplier>
<Supplier><address /><Phone /></Supplier>
<Supplier><address /><Phone /></Supplier>
</Suppliers>
return local:transform($d)
答案 1 :(得分:0)
这个XQuery:
declare variable $pPath as xs:string external;
declare variable $vPath := tokenize($pPath,'\|');
declare function local:copy-match($x as element()) {
element
{node-name($x)}
{for $child in $x/node()
return
if ($child instance of element())
then
local:match($child)
else
$child
}
};
declare function local:match($x as element()) {
let $element-path := string-join(
$x/ancestor-or-self::node()/name(),
'/'
)
where
not(
some $path in $vPath
satisfies
ends-with($element-path,$path)
)
return
local:copy-match($x)
};
local:match(/*)
将此xs:string
作为$pPath
参数:'Supplier/address|Supplier/Phone'
输出:
<Suppliers>
<Supplier></Supplier>
</Suppliers>