MarkLogic 12 EA 1 Product Documentation
xdmp:xslt-eval

xdmp:xslt-eval(
   $stylesheet as node(),
   [$input as node()?],
   [$params as map:map?],
   [$options as (element()|map:map)?]
) as document-node()*

Summary

Executes an XSLT stylesheet against a node.

Parameters
stylesheet The XSLT stylesheet to be executed.
input The context node to which the stylesheet is applied.
params 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 xdmp:key-from-QName is a convenient way to generate these keys. Each entry in the map is the value of the corresponding parameter.
options The options node. The default value is (). The node must be in the xdmp:eval namespace. See the xdmp:eval section for a list of options. Additional options include:

<mode>

A QName specifying the initial stylesheet mode to use (the <xsl:template> with the specified mode attribute).

<template>

A QName specifying the name of the initial template to apply.

Usage Notes

When creating the xsl:stylesheet element that is the stylesheet parameter to xdmp:xslt-eval , 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 the example below.

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).

Example

let $foo-to-bar :=
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
    <xsl:template match="foo">
      <bar>
        <xsl:apply-templates select="node()"/>
      </bar>
    </xsl:template>
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:template>
  </xsl:stylesheet>
return xdmp:xslt-eval($foo-to-bar,
  <stuff>
   <one/>
   <foo/>
   <two/>
   <foo><blah>42</blah></foo>
   <bar>22</bar>
  </stuff>)/element()

Example

xquery version "1.0-ml" ;

(: Hello World example for xslt:eval, with a parameter :)

let $params := map:map()
let $_put := map:put(
                    $params,
                    xdmp:key-from-QName(fn:QName("foo", "pName")),
                    "Stephen")
let $_put := map:put(
                    $params,
                    xdmp:key-from-QName(fn:QName("bar", "bName")),
                    "Ron")
let $_put := map:put(
                    $params,
                    "cName",
                    "Dave")
return
  xdmp:xslt-eval(
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:f="foo" xmlns:b="bar"
      version="2.0">
    <xsl:param name="f:pName"/>
    <xsl:param name="b:bName"/>
    <xsl:param name="cName"/>
    <xsl:param name="greeting" select="'Hi there '"/>
    <xsl:template match="/">
       <output>
         <xsl:copy-of select="node"/>
         <greeting><xsl:value-of select="$greeting"/></greeting>
         <param><xsl:value-of select="$f:pName"/></param>
         <param><xsl:value-of select="$b:bName"/></param>
         <param><xsl:value-of select="$cName"/></param>
       </output>
    </xsl:template>
  </xsl:stylesheet>,
  document { <node>Hello World</node> },
  $params)
=>
<?xml version="1.0" encoding="ASCII"?>
<output xmlns:f="foo" xmlns:b="bar">
  <node>Hello World</node>
  <greeting>Hi there </greeting>
  <param>Stephen</param>
  <param>Ron</param>
  <param>Dave</param>
</output>

Example

xquery version "1.0-ml" ;

(: example that passes in a QName for a mode :)
xdmp:xslt-eval(
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="2.0">
    <xsl:template match="/">
       <output>this has no mode</output>
    </xsl:template>
    <xsl:template match="/" mode="my-mode">
      <debug>this has a mode</debug>
    </xsl:template>
  </xsl:stylesheet>,
  document { <node>Hello World</node> },
  (),
  <options xmlns="xdmp:eval">
    <mode>{fn:QName("", "my-mode")}</mode>
  </options>)
=>
<?xml version="1.0" encoding="ASCII"?>
<debug>this has a mode</debug>

Example

xquery version "1.0-ml";

(:
  Note the esacped curly braces ( {{ and }} on the name attribute
  of xsl:element), as the stylesheet must first be parsed by XQuery
  before it is evaluated as a stylesheet.  If you do not escape
  the curly braces, the query throws the XQuery exception:
  [1.0-ml] XDMP-CONTEXT: (err:XPDY0002) Expression depends on the context
           where none is defined
  That is because, without the escaped braces, XQuery tries to evaluate
  the expression in the name attribute, but there is no context for it.
:)
xdmp:xslt-eval(
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="2.0">
    <xsl:template match="foo">
     <xsl:element name="{{name(.)}}"/>
    </xsl:template>
</xsl:stylesheet>
,
document{ <foo>something goes here</foo>} )
=>
<?xml version="1.0" encoding="ASCII"?>
<foo/>
Powered by MarkLogic Server | Terms of Use | Privacy Policy