<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/cts.highlight.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>cts.highlight</api:function-name><api:suggest>cts.highlight</api:suggest><api:suggest>cts</api:suggest><api:suggest>highlight</api:suggest><api:function mode="javascript" name="highlight" 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.highlight"><api:summary>
  Returns a copy of the node, replacing any text matching the query
  with the specified expression.  You can use this function
  to easily highlight any text found in a query. Unlike
  <code xmlns="http://www.w3.org/1999/xhtml">fn.replace</code> and other string functions that match
  literal text, <code xmlns="http://www.w3.org/1999/xhtml">cts.highlight</code> matches every term that
  matches the search, including stemmed matches or matches with
  different capitalization.
</api:summary><api:params><api:param name="node" type="node()"><api:param-description>
    A node to highlight.  The node must be either a document node,
    element node, object node, or array 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 to highlight.  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(NodeBuilder, xs:string, text(), cts:query*, xs:integer) as xs:string?"><api:param-description>
    A function to call on each match.
  </api:param-description><api:param-name>callback</api:param-name><api:param-type>function(NodeBuilder, xs.string, text(), cts.query*, xs.integer) as xs.string?</api:param-type></api:param><api:param name="builder" type="NodeBuilder"><api:param-description>
    The builder that will be used to construct the highlighted copy.
  </api:param-description><api:param-name>builder</api:param-name><api:param-type>NodeBuilder</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>builder</code> as <code>NodeBuilder</code></dt>
    <dd><p>An Node builder that is building the highlighted node copy. 
    Whetever the callback adds to the builder will be added to the final copy.
    </p></dd>
    <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)) = 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:</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 all the builder results .</dd>
      <dt>"skip"</dt>
      <dd>Skip walking any more matches and return all the builder results.</dd>
      <dt>"break"</dt>
      <dd>Stop walking matches and return all the builder results.</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.highlight</code> to highlight results matching
 <code>cts:similar-query</code> and <code>cts:element-attribute-*-query</code>
 items.  Using <code>cts.highlight</code> with these queries will
 return the nodes without any highlighting. </p>
 <p xmlns="http://www.w3.org/1999/xhtml">You can also use <code>cts.highlight</code> as a general search
 and replace function. The specified expression will replace any matching
 text. For example, you could replace the word "hello" with "goodbye"
 in a query similar to the following:</p>
 <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
 cts.highlight(node, "hello", 
	function(builder, text, node, queries, start) {
	builder.addText("goodbye");
	});
</pre>
</api:usage><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// To highlight "MarkLogic" with bold in the following paragraph:

var doc = new NodeBuilder();
doc.startElement("p");
doc.addText("MarkLogic Server is an enterprise-class\n\
  database specifically built for content.");
doc.endElement();

var result = new NodeBuilder();
cts.highlight(doc.toNode(), "MarkLogic", 
  function(builder,text,node,queries,start) {
    builder.startElement("b");
    builder.addText(text);
    builder.endElement(); }, result);
result.toNode()

=&gt;

  &lt;p&gt;&lt;b&gt;MarkLogic&lt;/b&gt; Server is an enterprise-class
  database specifically built for content.&lt;/p&gt;

</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// Similar to the above use case but on JSON nodes. Note that
// an array is used to represent the original text with the
// specified object node as a member.

var x = {"p1":
  "MarkLogic Server is an enterprise-class database built for content."};
var result = new NodeBuilder();
cts.highlight(x, "MarkLogic", 
  function(builder,text,node,queries,start) {
    builder.addNode( {"highlighted" : text} );
  }, result
);
result.toNode();

=&gt;

  {
    "p1": [
      {
        "highlighted": "MarkLogic"
      },
     " Server is an enterprise-class database built for content."
    ]
  }

</pre></api:example><api:example> <pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// Also on JSON nodes but the query matches the whole node. In this case,
// no array node is created.

var x = { "p1" : "MarkLogic Server"};
var result = new NodeBuilder();
cts.highlight(x, "MarkLogic Server", 
  function(builder,text,node,queries,start) {
    builder.addNode( {"highlighted" : text} )
  }, result
);
result.toNode();

Returns:

  {
    "p1": {
      "highlighted": "MarkLogic Server"
    }
  }

</pre></api:example><api:example><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// Given the following document with the URI "hellogoodbye.xml":

&lt;root&gt;
  &lt;a&gt;It starts with hello and ends with goodbye.&lt;/a&gt;
&lt;/root&gt;

******

// The following query will highlight the word "hello" in
// blue, and everything else in red.

var result = new NodeBuilder();
cts.highlight(fn.doc("hellogoodbye.xml").next().value,
     cts.andQuery([cts.wordQuery("hello"),
                   cts.wordQuery("goodbye")]),
  function(builder, text, node, queries, start) {
    builder.startElement("font");
    if (cts.wordQueryText(queries) == "hello") {
      builder.addAttribute("color","blue"); 
      builder.addText(text);
      builder.endElement(); }
    else {
      builder.addAttribute("color","red");
      builder.addText(text);
      builder.endElement(); }
  }, result
);
result.toNode();

=&gt;

&lt;root&gt;
  &lt;a&gt;It starts with &lt;font color="blue"&gt;hello&lt;/font&gt;
  and ends with &lt;font color="red"&gt;goodbye&lt;/font&gt;.&lt;/a&gt;
&lt;/root&gt;
</pre></api:example><api:example><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var results = [];
for (var x of cts.search("MarkLogic")) {
  var result = new NodeBuilder();
  cts.highlight(x,"MarkLogic",
    function(builder, text, node, queries, start) {
      builder.startElement("b");
      builder.addText(text);
      builder.endElement();
    }, result);
  results.push(result.toNode());
};
results;

returns an array with all of the nodes that contain "MarkLogic",
placing bold markup (&lt;b&gt; tags) around the matched words.
</pre></api:example></api:function></api:function-page>