【问题标题】:How to create delete function in XQuery如何在 XQuery 中创建删除函数
【发布时间】:2010-03-06 18:37:58
【问题描述】:

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

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>

但我希望结果为&lt;Suppliers&gt;&lt;Supplier&gt;&lt;/Supplier&gt;&lt;/Suppliers&gt;。 有什么方法可以编辑我的功能代码以删除那些必要的标签吗?

【问题讨论】:

    标签: xml function xquery


    【解决方案1】:

    您可以尝试使用以下递归函数来删除这些所需元素。

    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)
    

    【讨论】:

      【解决方案2】:

      这个 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>
      

      【讨论】:

        猜你喜欢
        • 2015-06-28
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-28
        • 2010-10-13
        • 2021-06-30
        相关资源
        最近更新 更多