# XQuery/Filtering Nodes
## Motivation

You want to create filters that remove or replace specific nodes in an XML stream. This stream may be in-memory XML documents and may not be on-disk.

## Method

To process all nodes in a tree we will start with recursive function called the identity transform. This function copies the source tree into the output tree without change. We begin with this process and then add some exception processing for each filter.
This function uses an XQuery construct called computed element constructor to construct an element. The format of the element constructor is the following:
In the above case ELEMENT-VALUE is another query that finds all the child elements of the current node. The for loop selects all nodes of the current element and does the following pseudo-code:
If you understand this basic structure of this algorithm you can now modify it to filter out only the elements you want. You just start with this template and modify various sections.
Note that you can also achieve this function by using the typeswitch operator:

## Removing all attributes

The following function removes all attributes from elements since attributes are not copied.
This function can also be arrived at by using the typeswitch operator:
The function can be parameterized by adding a second function argument to indicate what attributes should be removed.

## Replacing all attribute values

For all elements that have specific attribute names replace old attribute values with new attribute value.

## Removing named attributes

Attributes are filtered in the predicate expression not(name=$attribute-name) so that named attributes are omitted.

## Removing named elements

Likewise, elements can be filtered in a predicate:
This adds the node qualifier and the name of the node in the predicate:
To use this function just pass the input XML as the first parameter and a sequence of element names as strings as the second parameter. For example:

## Renaming Elements Using a Map

Suppose we have a file of elements that we want to rename using a filter. We want to store the rename rules in a file like this:
The rename elements function is the following
The following is the input and output

## Removing Empty Elements

Many RDBMS systems export rows of data that are converted into XML. It is a best practice to remove any XML elements that have no text content.
The XQuery function will take in a single element and it returns an optional element.
The first test is to check if a child element or text is present. If it is then an element is constructed and the attribute are added. Then for each child element the function calls itself.
Example Input
Example Output
Note that even if an element contains spaces, carriage returns or tabs, the element will be removed.

## Example illustrating the above filters

The following script demonstrates these functions:
Run

## Adding a Namespace

Here is a function that will add a namespace to the root element in an XML document. Note that it uses an element constructor with two parameters. The first parameter is the element name and the second is the element content. The element name is created using the QName function and the element content is created using {$in/@*, $in/node}, which will add attributes and all child nodes.
If we use the following input:
The function will return the following output:
Note that if you use null as the prefix, then the prefix will not be used in the root element, However, the namespace will be used.

## Removing unwanted namespaces

Some systems do not allow you to have precise control of the namespaces used after doing an update despite the use of copy-namespaces declarations.

### Remove TEI Namespace

The following XQuery function is an example that will remove the TEI namespace from a node.
Below two functions will remove any namespace from a node, nnsc stands for no-namespace-copy. The first one performs much faster: From my limited understanding it jumps attributes quicker. The other one still here, something tricky might be hidden there.

### Remove ALL Namespaces

The following recursive function will remove all namespaces from elements and attributes. Note that the local-name function is used to generate the namespace free attribute name.
Version 2
This version uses @* and * to generate a single sequence of attributes and elements. Elements are passed a recursive function but attributes are returned directly as $child.
Conversely, if you want to add a namespace to an element, a starting point in this Misztur, Chrisblog post: http://fgeorges.blogspot.com/2006/08/add-namespace-node-to-element-in.html

## Remove extra whitespace

Contributed by Chris Misztur.

## Removing elements with no string value

Elements which contain no string value or which contain whitespace only can be removed:

## Removing empty attributes

Attributes which contain no text can be stripped:

## One Function for Several In-Memory Operations

You can integrate several functions for altering the node tree in one function. In the following function a number of common operations on elements are facilitated.
The parameters passed are 1) the node tree to be operated on, 2) any new item(s) to be inserted, 3) the action to be performed, 4) the name(s) of the element(s) targeted by the action.
The function can insert one or more elements supplied as a parameter in a certain position relative to (before or after or as the first or last child of) target elements in the node tree.
One or more elements can be inserted in the same position as the target element(s), i.e. they can substitute for them.
If the action is 'remove', the target element(s) are removed. If the action is 'remove-if-empty', the target element(s) are removed if they have no (normalized) string value. If the action is 'substitute-children-for-parent', the target element(s) are substituted by their child element(s). (In the last three cases the new content parameter is not consulted and should, for clarity, be the empty sequence).
If the action to be taken is 'change-name', the name of the element is changed to the first item of the new content.
If the action to be taken is 'substitute-content', any children of the target element(s) are substituted with the new content.
Note that context-free functions, for instance current-date, can be passed as new content.
A typeswitch is not used because it requires static parameters.
Having the following main,
the result will be
Note that if the target element is 'p', the $new-node will be inserted in relation to every element named 'p'.
You can fill in any functions you need and delete those that you do not need.
This following function facilitates several operations on attributes. This is more complicated than working with elements, for element names have to be considered as well.
The parameters passed are 1) the node tree to be operated on, 2) a new attribute name, 3) the new attribute contents, 4) the action to be performed, 5) the name(s) of the element(s) targeted by the action, 6) the name(s) of the attribute(s) targeted by the action.
By just using the action parameter, you can remove all empty-attributes.
If you wish to remove all named attributes, you need to supply the name of the attribute to be removed.
If you wish to change all values of named attributes, you need to supply the new value as well.
If you wish to attach an attribute, with name and value, to a specific element, you need to supply parameters for the element the attribute is to be attached to, the name of the attribute, and the value of the attribute, as well as the action.
If you wish to remove an attribute from a specific element, you need to supply parameters for the element the attribute is to be removed from, the name of the attribute, as well as the action.
If you wish to change the name of an attribute attached to a specific element, you need to supply parameters for the element the attribute is attached to, the name the attribute has, the new the attribute is to have, as well as the action.
Having the following main,
the result will be

## References

W3C page on computed element constructors