<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.walk.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.walk</api:function-name><api:suggest>cts.walk</api:suggest><api:suggest>cts</api:suggest><api:suggest>walk</api:suggest><api:function mode="javascript" name="walk" type="builtin" lib="cts" category="SearchBuiltins" subcategory="Search" class="javascript" hidden="false" bucket="MarkLogic Built-In Functions" prefix="cts" namespace="http://marklogic.com/cts" fullname="cts.walk"><api:summary>
  Walks a node, evaluating a callback function for any text matching a query.
  It returns the empty-sequence().
  This is similar to <code xmlns="http://www.w3.org/1999/xhtml">cts.highlight</code> in how it
  evaluates its expression, but it is different in what it returns.
</api:summary><api:params><api:param name="node" type="node()"><api:param-description>
    A node to walk.  The node must be either a document node
    or an element node; it cannot be a text node.
  </api:param-description><api:param-name>node</api:param-name><api:param-type>Node</api:param-type></api:param><api:param name="query" type="cts:query"><api:param-description>
   A query specifying the text on which to evaluate the expression.
   If a string is entered, the string is treated as a
   <code xmlns="http://www.w3.org/1999/xhtml">cts:word-query</code> of the specified string.
  </api:param-description><api:param-name>query</api:param-name><api:param-type>cts.query</api:param-type></api:param><api:param name="callback" type="function(xs:string, text(), cts:query*, xs:integer) as xs:string?"><api:param-description>
    A function to call with matching text.
  </api:param-description><api:param-name>callback</api:param-name><api:param-type>function(xs.string, text(), cts.query*, xs.integer) as xs.string?</api:param-type></api:param></api:params><api:return>null</api:return><api:usage>
  <p xmlns="http://www.w3.org/1999/xhtml">
  The arguments to the callback function provide context for the match.
  </p>
  <blockquote xmlns="http://www.w3.org/1999/xhtml"><dl>
    <dt><code>text</code> as <code>xs:string</code></dt>
    <dd><p>The matched text.</p></dd>
    <dt><code>node</code> as <code>text()</code></dt>
    <dd><p>The node containing the matched text.</p></dd>
    <dt><code>queries</code> as <code>cts:query*</code></dt>
    <dd><p>The matching queries.</p></dd>
    <dt><code>start</code> as <code>xs:integer</code></dt>
    <dd><p>The string-length position of the first character of
    <code>text</code> in <code>node</code>.  Therefore, the following
    always returns true:</p>
    <pre xml:space="preserve">fn.substring(node, start,
             fn.stringLength(text)) eq text </pre>
    </dd>
 </dl></blockquote>
 <p xmlns="http://www.w3.org/1999/xhtml">The return from the callback function is an action that specifies what 
happens next in the walk:</p>
    <dl xmlns="http://www.w3.org/1999/xhtml">
      <dt>"continue"</dt>
      <dd>(default) Walk the next match.
      If there are no more matches, return.</dd>
      <dt>"skip"</dt>
      <dd>Skip walking any more matches and return.</dd>
      <dt>"break"</dt>
      <dd>Stop walking matches and return.</dd>
      <dt>null</dt>
      <dd>Continue with the previous action.</dd>
    </dl>
 <p xmlns="http://www.w3.org/1999/xhtml">You cannot use <code>cts.walk</code> to walk results matching
 <code>cts:similar-query</code> and <code>cts:element-attribute-*-query</code>
 items.</p>
 <p xmlns="http://www.w3.org/1999/xhtml">The callback function can use variables in scope to accumulate results.
</p>
</api:usage><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//
//   Return all text nodes containing matches to the query "the".
//
var doc = new NodeBuilder();
doc.startElement("p");
doc.addText("the quick brown fox ");
doc.startElement("b"); doc.addText("jumped"); doc.endElement();
doc.addText(" over the lazy dog's back");
doc.endElement();

var results = [];
function callback(text, node, queries, start) {
  results.push(node);
  return "continue";
};

cts.walk(doc.toNode(), "the", callback);
results;
=&gt;
  ["the quick brown fox ", " over the lazy dog's back"]

</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//
//  Do not show any more matches that occur after
//  $threshold characters.
//
var doc = new NodeBuilder();
doc.startElement("p");
doc.addText("This is 1, this is 2, this is 3, this is 4, this is 5.");
doc.endElement();
var pos = 1;
var threshold = 20;
var results = [];

function callback(text, node, queries, start) {
  if (pos &gt; threshold )
     return "break";
  else {
    results.push(text);
    pos = start;
    return "continue";
  }
}

cts.walk(doc.toNode(), "this is", callback);
results;

=&gt;
["This is", "this is", "this is"]
</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//
//   Show the first two matches.
//
var doc = new NodeBuilder();
doc.startElement("p");
doc.addText("This is 1, this is 2, this is 3, this is 4, this is 5.");
doc.endElement();
var match = 0;
var threshold = 2;
var results = [];

function callback(text, node, queries, start) {
  if (match &gt;= threshold )
     return "break";
  else {
    results.push(text);
    match = match + 1;
    return "continue";
  }
}

cts.walk(doc.toNode(), "this is", callback);
results;

=&gt;
["This is", "this is"]
</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//
//  Similar to the example above but on JSON nodes.
//
var x = ["This is 1, this is 2, this is 3, this is 4, this is 5."];
var match = 0;
var threshold = 2;
var results = [];

function callback(text, node, queries, start) {
  if (match &gt;= threshold )
     return "break";
  else {
    results.push(text);
    match = match + 1;
    return "continue";
  }
}

cts.walk(x, "this is", callback);
results;
=&gt;
["This is", "this is"]
</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//
//  It works on JSON number, boolean and null nodes as well.
//
var x = { "p1" : "There are ", "p2" : 10,  "p3" : " books here."};
var results = [];
function callback (text, node, queries, start) {
  results.push(node);
  return "continue";
}
cts.walk(x, cts.jsonPropertyValueQuery("p2",10), callback);
results;
=&gt;
[10]
</pre></api:example></api:function></api:function-page>