Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在XQuery中创建删除函数_Xml_Function_Xquery - Fatal编程技术网

Xml 如何在XQuery中创建删除函数

Xml 如何在XQuery中创建删除函数,xml,function,xquery,Xml,Function,Xquery,我在尝试创建删除函数时遇到了一个问题。我目前的代码是: 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 qsx

我在尝试创建删除函数时遇到了一个问题。我目前的代码是:

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和$p=/Suppliers/supplier/* 结果是:

<Suppliers><Supplier><address /><Phone /></Supplier></Suppliers>
但我希望结果是这样的。
有没有办法编辑我的函数代码来删除那些必要的标记?

您可以尝试以下递归函数来删除那些所需的元素

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)
此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>