Primitive operations revisited

delete nodes

Syntax:

delete node location


delete nodes location 

The expression location represents a sequence of nodes which are marked for deletion (the actual number of nodes does not need to match the keyword node or nodes).

insert nodes

Syntax:

insert (node|nodes) items into path


insert (node|nodes) items as first into Path


insert (node|nodes) items as last into Path


insert (node|nodes) items before Path


insert (node|nodes) items after Path
The expression Path must point to a single target node.

The expression items must yield a sequence of items to insert relatively to the target node.

Notice that even though the keyword node or nodes is used, the inserted items can be non-node items. What happens actually is that the string values of non-node items are concatenated to form text nodes.

  • If either form of into is used, then the target node must be an element or a document. The items to insert are treated exactly as the contents of an element constructor.

    For example if $target points to an empty element <CONT/>,

    insert nodes (attribute A { 5.4 }, <child1/>, "text", 2 to 4) 
    into $target
    yields:
    <CONT A="5.4"><child1/>text 2 3 4</CONT>

    Therefore the same rules as in constructors apply: item order is preserved, a space is inserted between consecutive non-node items, inserted nodes are copied first, attribute nodes are not allowed after other item types, etc.

  • When the keywords as first (resp. as last) are used, the items are inserted before (resp. after) any existing children of the element.

    For example if $target points to an element <parent><kid/></parent>

    insert node <elder/> as first into $target

    yields:

    <parent><elder/><kid/></parent>

    When the only keyword into is used, the resulting position is implementation dependent. It is only guaranteed that as first into and as last into have priority over into.

  • If before or after are used, any node type is allowed for the target node.

  • Attributes are a special case: regardless of the before or after keyword used, attributes are always inserted into the parent element of the target. The order of inserted attributes is unspecified. Name conflicts can generate errors.

replace node

Syntax:

replace node location with items

The expression location must point to a single target node.

The expression items must yield a sequence of items that will replace the target node.

  • Except for document and attribute node types, the target node can be replaced by any sequence of items. The replacing items are treated exactly as the contents of an element/document constructor.

    For example if $target points to an element <P><kid/>some text</P>,

    replace node $target/kid with "here is" 

    yields:

    <P>here is some text</P>
  • Attributes are a special case: they can only be replaced by an attribute node. Name conflicts can generate errors.

replace value of node

Syntax:

replace value of node location with items

Here the identity of the target node is preserved. Only its value or contents (for an element or a document) is replaced.

  • If the target is an element or a document node, then all its former children are removed and replaced. The replacing items are treated exactly as the contents of a text constructor (so all node items are replaced by their string-value).

    For example if $target points to an element <P><kid/>some text</P>,

    replace value of node $target with (<text>let's count: </text>, 1 to 3, "...") 
    yields:
    <P>let's count: 1 2 3 ...</P>


    So the element contents are replaced by a text node whose value is the concatenation of the string values of replacing items.

  • If the target node is a leaf node (attribute, text, comment, processing-instruction) then its string value is replaced by the concatenation of the string values of replacing items.

    For example if $target points to an element <P order="old">some text</P>,

    replace value of node $target/@order with (1 to 3, <ell>...</ell>) 

    yields:

    <P order="1 2 3...">some text</P>

rename node

Syntax:

rename node location as name-expression

The expression location must point to a single target element, attribute or processing-instruction.

The expression name-expression must yield a single QName or string item.

For example if $target points to an element <CONT B="b">some text</CONT>

rename node $target as qName("some.namespace", "CONTAINER"),
rename node $target/B as "NEWB"

yields:

<ns1:CONTAINER NEWB="b" xmlns:ns1="some.namespace">some text</ns1:CONTAINER>


transform

Syntax:

copy $var := node [, $var2 := node2 ...] 
modify updating-expression 
return expression

Each node expression is copied (at least virtually) and bound to a variable.

The updating-expression contains or invokes one or several update primitives. These primitives are allowed to act only upon the copied XML trees, pointed by the bound variables. Therefore the transform expression has no side effect.

Before the return expression is evaluated, all updates are applied to the copied trees. Typically the return expression would be a bound variable, or a node constructor involving the bound variables, so it will yield the updated tree(s).

For example if $target points to an element

copy $target := <CONT id="001">some text</CONT>
modify (
   rename node $target as "SECTION",
   insert node <TITLE>The title</TITLE> as first into $target
)
return element DOC { $target }

returns:

<DOC><SECTION id="001"><TITLE>The title</TITLE>some text</SECTION></DOC>

Copyright © 2010 - 2023 Emakin. All rights reserved.