Project

Profile

Help

XQuery - Copy xml and skip element

Added by Anonymous about 15 years ago

Legacy ID: #7030162 Legacy Poster: NickDiamond (nickdiamond_80)

Hi, I am searching for a XQuery function that copies a xml-file, but skips a specified element (or maybe more elements). It should look like this : <xml> <element1> <element2> <element3> </element3> </element2> </element1> then "function copy-and-skip(file/xml, 'element2') with the result : <xml> <element1> <element3> </element3> </element1> What I have at the moment is the following : declare function copy-skip-elements ($element as element() , $element-name as xs:string*) as element() { element {node-name($element) } { $element/@*, for $child in $element/node()[not(name(.)=$element-name)] return if ($child instance of element()) then copy-skip-elements($child,$element-name) else $child } }; But this not only removes the given element, but also the childelements. But I want keep the child-elements. Any hints for this ?


Replies (1)

RE: XQuery - Copy xml and skip element - Added by Anonymous about 15 years ago

Legacy ID: #7030257 Legacy Poster: Michael Kay (mhkay)

This kind of thing is SO much easier in XSLT! A good place for XQuery programming advice is talk (at) x-query.com. This list is provided for Saxon-specific problems, not for general programming advice. Your code isn't doing anything with the elements that match $element-name, it should apply copy-skip-elements to their children. Something like for $child in $element/node() return if ($child instance of element()) then if name($child)=$element-name) then for $grandchild in $child/node() return copy-skip-elements($grandchild,$element-name) else copy-skip-elements($child,$element-name) else $child

    (1-1/1)

    Please register to reply