xdmp.xsltEval( stylesheet as Node, [input as Node?], [params as Object?], [options as Object?] ) as Sequence
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.keyFromQName
is a convenient way to generate these keys.
Each entry in the map is the value of the corresponding parameter.
|
options |
The options object. The default value is null.
Additional options include:
|
When creating the xsl:stylesheet
element that is the
stylesheet parameter to
xdmp.xsltEval
,
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).
var fooToBar = fn.head(xdmp.unquote( ' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\ version="2.0">\n\ <xsl:template match="foo">\n\ <bar>\n\ <xsl:apply-templates select="node()"/>\n\ </bar>\n\ </xsl:template>\n\ <xsl:template match="@*|node()">\n\ <xsl:copy>\n\ <xsl:apply-templates select="@*|node()"/>\n\ </xsl:copy>\n\ </xsl:template>\n\ </xsl:stylesheet>')); xdmp.xsltEval(fooToBar, fn.head(xdmp.unquote( ' <stuff>\n\ <one/>\n\ <foo/>\n\ <two/>\n\ <foo><blah>42</blah></foo>\n\ <bar>22</bar>\n\ </stuff>')).root); => <?xml version="1.0" encoding="UTF-8"?> <stuff> <one/> <bar/> <two/> <bar> <blah>42</blah> </bar> <bar>22</bar> </stuff>
// 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(fn.head(xdmp.unquote( ' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\ xmlns:f="foo" xmlns:b="bar"\n\ version="2.0">\n\ <xsl:param name="f:pName"/>\n\ <xsl:param name="b:bName"/>\n\ <xsl:param name="cName"/>\n\ <xsl:param name="greeting" select="' + "'Hi there '" +' "/>\n\ <xsl:template match="/">\n\ <output>\n\ <xsl:copy-of select="node"/>\n\ <greeting><xsl:value-of select="$greeting"/></greeting>\n\ <param><xsl:value-of select="$f:pName"/></param>\n\ <param><xsl:value-of select="$b:bName"/></param>\n\ <param><xsl:value-of select="$cName"/></param>\n\ </output>\n\ </xsl:template>\n\ </xsl:stylesheet>')), fn.head(xdmp.unquote('<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 that passes in a QName for a mode xdmp.xsltEval(fn.head(xdmp.unquote( ' <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"\n\ version="2.0">\n\ <xsl:template match="/">\n\ <output>this has no mode</output>\n\ </xsl:template>\n\ <xsl:template match="/" mode="my-mode">\n\ <debug>this has a mode</debug>\n\ </xsl:template>\n\ </xsl:stylesheet>')), fn.head(xdmp.unquote('<node>Hello World</node>')), null, {mode:"{}my-mode"}); => <?xml version="1.0" encoding="ASCII"?> <debug>this has a mode</debug>
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.