home contribute faq download

FunctX XQuery Functions

functx:remove-elements-not-contents

Removes descendant XML elements but keeps their content

Google
Webdatypic.com

Description

The functx:remove-elements-not-contents function removes descendant elements from all of the nodes in $nodes based on name, but keeps the contents of the removed elements. This is useful, for example, for removing in-line formatting. The $names argument is a sequence of strings that represent element names to remove. Prefixes can (and must) be used in the $names values for elements that are prefixed in the input documents. You can also specify wildcard values "*", "*:" and ":*" in the second argument. See the description for functx:name-test for details.

Note: this function is intended to change the way elements appear in the results of a query, not to update them in an XML database. To update your XML database, you should use the implementation-specific update functions of your processor.

Arguments and Return Type

NameTypeDescription
$nodes node()* the root(s) to start from
$names xs:string* the names of the elements to remove
return value node()*

XQuery Function Declaration

See XSLT definition.
declare namespace functx = "http://www.functx.com";
declare function functx:remove-elements-not-contents
  ( $nodes as node()* ,
    $names as xs:string* )  as node()* {

   for $node in $nodes
   return
    if ($node instance of element())
    then if (functx:name-test(name($node),$names))
         then functx:remove-elements-not-contents($node/node(), $names)
         else element {node-name($node)}
              {$node/@*,
              functx:remove-elements-not-contents($node/node(),$names)}
    else if ($node instance of document-node())
    then functx:remove-elements-not-contents($node/node(), $names)
    else $node
 } ;

Examples

let $in-xml-1 :=
<in-xml>
   <a>
      <b>b1</b>
      <c>c1</c>
   </a>
   <c>Mixed <b>content</b></c>
</in-xml>
let $in-xml-2 :=
<in-xml xmlns:x="http://x">
   <a>
      <b>b1</b>
      <c>c1</c>
   </a>
   <c>Mixed <x:b>content</x:b></c>
</in-xml>
return
XQuery ExampleResults
functx:remove-elements-not-contents(
     $in-xml-1, 'b')
<in-xml>
  <a>b1<c>c1</c>
  </a>
  <c>Mixed content</c>
</in-xml>
functx:remove-elements-not-contents(
     $in-xml-1, ('b','c'))
<in-xml>
  <a>b1c1</a>Mixed content
</in-xml>
functx:remove-elements-not-contents(
     $in-xml-1, 'a')
<in-xml>
  <b>b1</b>
  <c>c1</c>
  <c>Mixed <b>content</b>
  </c>
</in-xml>
functx:remove-elements-not-contents(
     $in-xml-2, 'x:b')
<in-xml>
  <a>
    <b>b1</b>
    <c>c1</c>
  </a>
  <c>Mixed content</c>
</in-xml>

Depends On

functx:name-testWhether a name matches a list of names or name wildcards

See Also

functx:remove-elementsRemoves child elements from an XML node, based on name
functx:remove-elements-deepRemoves descendant elements from an XML node, based on name

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, https://www.datypic.com
Datypic XQuery Services

Recommended Reading:

XQuery