如何在 XQuery 中创建删除函数

发布于 2024-08-24 02:36:26 字数 877 浏览 2 评论 0原文

当我尝试创建删除功能时遇到问题。我当前的代码是:

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>

但我希望结果为 。 有没有办法编辑我的功能代码以删除那些必要的标签?

I met a problem when I try to create delete function. My current code is:

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   
};

If the input are: $d = C:/supplier.xml and $p= /Suppliers/Supplier/*
the result is:

<Suppliers><Supplier><address /><Phone /></Supplier></Suppliers>

But I want the result to be <Suppliers><Supplier></Supplier></Suppliers>.
Is there any way to edit my function codes to remove those necessary tags as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

知足的幸福 2024-08-31 02:36:26

您可以尝试以下递归函数作为删除这些所需元素的方法。

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)

You could try the following recursive function as a way of deleting those desired elements.

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)
梦途 2024-08-31 02:36:26

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

This 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(/*)

With this xs:string as $pPath param: 'Supplier/address|Supplier/Phone'

Output:

<Suppliers>
    <Supplier></Supplier>
</Suppliers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文