<?xml version="1.0" encoding="UTF-8"?><api:function-page xml:base="/apidoc/8.0/xdmp.xsltEval.xml" generated="2015-10-07T16:36:00.016766-07:00" mode="javascript" xmlns:api="http://marklogic.com/rundmc/api"><api:function-name>xdmp.xsltEval</api:function-name><api:suggest>xdmp.xslteval</api:suggest><api:suggest>xdmp</api:suggest><api:suggest>xslteval</api:suggest><api:function-link mode="xquery" fullname="xdmp:xslt-eval">/apidoc/8.0/xdmp:xslt-eval.xml</api:function-link><api:function mode="javascript" name="xsltEval" type="builtin" lib="xdmp" category="Extension" subcategory="XQuery Context" hidden="false" bucket="MarkLogic Built-In Functions" prefix="xdmp" namespace="http://marklogic.com/xdmp" fullname="xdmp.xsltEval"><api:summary>
   Executes an XSLT stylesheet against a node.
</api:summary><api:params><api:param name="stylesheet" type="node()"><api:param-description>
    The XSLT stylesheet to be executed.
  </api:param-description><api:param-name>stylesheet</api:param-name><api:param-type>Node</api:param-type></api:param><api:param name="input" type="node()?" optional="true"><api:param-description>
    The context node to which the stylesheet is applied.
  </api:param-description><api:param-name>input</api:param-name><api:param-type>Node?</api:param-type></api:param><api:param name="params" type="map:map?" optional="true"><api:param-description>
    The stylesheet parameter values for this evaluation.
    Each key in the map is a string representing the name of the parameter
    in Clark notation: "{namespaceURI}localname". The function
    <a href="./xdmp:key-from-QName" xmlns="http://www.w3.org/1999/xhtml">xdmp:key-from-QName</a>
    is a convenient way to generate these keys.
    Each entry in the map is the value of the corresponding parameter.
  </api:param-description><api:param-name>params</api:param-name><api:param-type>Object?</api:param-type></api:param><api:param name="options" type="(element()|map:map)?" optional="true"><api:param-description>
  
  <span class="javascript" xmlns="http://www.w3.org/1999/xhtml">The options object. The default value is null.</span>
  Additional options include:
  <dl xmlns="http://www.w3.org/1999/xhtml">
  
  <dt class="javascript"><p>mode</p></dt>
  <dd>A QName <span class="javascript"> (in clark notation) </span> 
  specifying the
  initial stylesheet mode to use (the <code>&lt;xsl:template&gt;</code> with the
  specified <code>mode</code> attribute).</dd>
  
  <dt class="javascript"><p>template</p></dt>
  <dd>A QName <span class="javascript"> (in clark notation) </span> 
  specifying the name of the initial template to apply.</dd>
  </dl>
  </api:param-description><api:param-name>options</api:param-name><api:param-type>Object?</api:param-type></api:param></api:params><api:return>ValueIterator</api:return><api:usage>
  <p xmlns="http://www.w3.org/1999/xhtml">When creating the <code>xsl:stylesheet</code> element that is the
  stylesheet parameter to <code>xdmp:xslt-eval</code>, keep in mind that
  it has to first be parsed by XQuery before
  it is evaluated as a stylesheet.  Therefore, any characters in the stylesheet
  that require escaping in XQuery must be escaped, otherwise you get an error
  in the XQuery.  For example, if the stylesheet has any curly braces
  ( { or } ), you must escape the curly braces (with curly braces). For
  an example, see <a href="#xslteval4">the example</a> below.</p>
  <p xmlns="http://www.w3.org/1999/xhtml">When running an XSLT stylesheet in MarkLogic, you pass in a node on
  which the stylesheet operates.  Many stylesheets are written
  to expect the initial node to be a document
  node.  In other XSLT processors, the node you pass to the stylesheet is
  typically read in from the filesystem and is always treated as a document
  node.  In MarkLogic, you often get the node to pass to the stylesheet as
  the result of a query or a search, and the node  is not necessarily a
  document node.  Therefore, if your stylesheet expects
  the context node to be a document node, make sure to pass in a document
  node and not an element node.  If you pass in an element node to a
  stylesheet that has default template rules to expect a document node,
  then you might miss the processing on the element you passed
  in (because the stylesheet might expect the child node to be the root
  element of the XML document, but if you passed in the root element instead of
  its parent document node, then the child nodes would be the children of the
  root element, causing the root element to miss its default processing).</p>
</api:usage><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
var fooToBar = xdmp.unquote(
'  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\
                  version="2.0"&gt;\n\
    &lt;xsl:template match="foo"&gt;\n\
      &lt;bar&gt;\n\
        &lt;xsl:apply-templates select="node()"/&gt;\n\
      &lt;/bar&gt;\n\
    &lt;/xsl:template&gt;\n\
    &lt;xsl:template match="@*|node()"&gt;\n\
      &lt;xsl:copy&gt;\n\
        &lt;xsl:apply-templates select="@*|node()"/&gt;\n\
      &lt;/xsl:copy&gt;\n\
    &lt;/xsl:template&gt;\n\
  &lt;/xsl:stylesheet&gt;').next().value;
xdmp.xsltEval(fooToBar, xdmp.unquote(
'  &lt;stuff&gt;\n\
   &lt;one/&gt;\n\
   &lt;foo/&gt;\n\
   &lt;two/&gt;\n\
   &lt;foo&gt;&lt;blah&gt;42&lt;/blah&gt;&lt;/foo&gt;\n\
   &lt;bar&gt;22&lt;/bar&gt;\n\
  &lt;/stuff&gt;').next().value.root);
=&gt;
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;stuff&gt;
   &lt;one/&gt;
   &lt;bar/&gt;
   &lt;two/&gt;
   &lt;bar&gt;
    &lt;blah&gt;42&lt;/blah&gt;
   &lt;/bar&gt;
   &lt;bar&gt;22&lt;/bar&gt;
&lt;/stuff&gt;
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
//  Hello World example for xdmp.xsltEval, with a parameter 

var params = new Object();
var key1 = xdmp.keyFromQName(fn.QName("foo", "pName"));
params.key1 = "Stephen";
var key2 = xdmp.keyFromQName(fn.QName("bar", "bName"));
params.key2 = "Ron";
params.cName = "Dave";
xdmp.xsltEval(xdmp.unquote(
'    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\
      xmlns:f="foo" xmlns:b="bar"\n\
      version="2.0"&gt;\n\
    &lt;xsl:param name="f:pName"/&gt;\n\
    &lt;xsl:param name="b:bName"/&gt;\n\
    &lt;xsl:param name="cName"/&gt;\n\
    &lt;xsl:param name="greeting" select="' + "'Hi there '" +' "/&gt;\n\
    &lt;xsl:template match="/"&gt;\n\
       &lt;output&gt;\n\
         &lt;xsl:copy-of select="node"/&gt;\n\
         &lt;greeting&gt;&lt;xsl:value-of select="$greeting"/&gt;&lt;/greeting&gt;\n\
         &lt;param&gt;&lt;xsl:value-of select="$f:pName"/&gt;&lt;/param&gt;\n\
         &lt;param&gt;&lt;xsl:value-of select="$b:bName"/&gt;&lt;/param&gt;\n\
         &lt;param&gt;&lt;xsl:value-of select="$cName"/&gt;&lt;/param&gt;\n\
       &lt;/output&gt;\n\
    &lt;/xsl:template&gt;\n\
  &lt;/xsl:stylesheet&gt;').next().value,
    xdmp.unquote('&lt;node&gt;Hello World&lt;/node&gt;').next().value,
    params);

=&gt;
&lt;?xml version="1.0" encoding="ASCII"?&gt;
&lt;output xmlns:f="foo" xmlns:b="bar"&gt;
  &lt;node&gt;Hello World&lt;/node&gt;
  &lt;greeting&gt;Hi there &lt;/greeting&gt;
  &lt;param&gt;Stephen&lt;/param&gt;
  &lt;param&gt;Ron&lt;/param&gt;
  &lt;param&gt;Dave&lt;/param&gt;
&lt;/output&gt;
</pre></api:example><api:example class="javascript"><pre xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
// example that passes in a QName for a mode 

xdmp.xsltEval(xdmp.unquote(
'    &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\
     version="2.0"&gt;\n\
    &lt;xsl:template match="/"&gt;\n\
       &lt;output&gt;this has no mode&lt;/output&gt;\n\
    &lt;/xsl:template&gt;\n\
    &lt;xsl:template match="/" mode="my-mode"&gt;\n\
      &lt;debug&gt;this has a mode&lt;/debug&gt;\n\
    &lt;/xsl:template&gt;\n\
  &lt;/xsl:stylesheet&gt;').next().value,
  xdmp.unquote('&lt;node&gt;Hello World&lt;/node&gt;').next().value,
  null,
  {mode:"{}my-mode"});

=&gt;
&lt;?xml version="1.0" encoding="ASCII"?&gt;
&lt;debug&gt;this has a mode&lt;/debug&gt;

</pre></api:example></api:function></api:function-page>