Loading TOC...

cts.elementWalk

cts.elementWalk(
   node as Node,
   element as xs.QName[],
   callback as function(NodeBuilder, node()) as xs.string?,
   builder as NodeBuilder
) as null

Summary

Returns a copy of the node, replacing any elements found with the specified expression.

Parameters
node A node to run the walk over. The node must be either a document node or an element node; it cannot be a text node.
element The name of elements to replace.
callback A function to call on each match.
builder The builder that will be used to construct the modified copy.

Usage Notes

The arguments to the callback function provide context for the element match.

builder as NodeBuilder

An Node builder that is building the highlighted node copy. Whatever the callback adds to the builder will be added to the final copy.

node as element()

The matching element node.

The return from the callback function is an action that specifies what happens next:

"continue"
(default) Walk the next match. If there are no more matches, return all evaluation results.
"skip"
Skip walking any more matches and return all evaluation results.
"break"
Stop walking matches and return all evaluation results.
null
Continue with the previous action.

Example

//
//  Replace every 'name' element with the text "Mary"
//
var x = new NodeBuilder();
x.startElement("p");
x.addText("Dear ");
x.startElement("name"); x.endElement();
x.addText( ", thank you for your interest.");
x.endElement();
var result = new NodeBuilder();
cts.elementWalk(x.toNode(), xs.QName("name"),
  function(builder,node) {
    builder.addText("Mary")
  },
  result
);
result.toNode()
=>
<p>Dear Mary, thank you for your interest.</p>

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.