Changes

Jump to navigation Jump to search
1,114 bytes added ,  13:12, 2 July 2020
no edit summary
This [[Module Library|XQuery Module]] contains functions and variables to perform XSLT XSL transformations. By default, this module uses Java’s XSLT 1.0 Xalan implementation to transform documents. XSLT 23.0 is used instead will be enabled if Version 9.x of the [httphttps://www.saxonica.com/ Saxon XSLT Processor] ({{Code|saxon9he.jar}}, {{Code|saxon9pe.jar}}, {{Code|saxon9ee.jar}}) is found in the classpath(see [[Startup#Distributions|Distributions]] for more details. A custom transformer can be specified by overwriting the system property {{Code|javax.xml.transform.TransformerFactory}}, as shown in the following Java example: <syntaxhighlight lang="java">System.setProperty( "javax.xml.transform.TransformerFactory", "org.custom.xslt.TransformerFactoryImpl");
<pre class="brush:java">
System.setProperty("javax.xml.transform.TransformerFactory", "org.custom.xslt.TransformerFactoryImpl");
Context ctx = new Context();
String result = new XQuery("xslt:transform('...', '...')").execute(ctx);
...
ctx.close();
</presyntaxhighlight>
=Conventions=
All functions and errors in this module are assigned to the {{Code|<code><nowiki>http://basex.org/modules/xslt}} </nowiki></code> namespace, which is statically bound to the {{Code|xslt}} prefix.<br/>All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.=Functions=
=Variables=xslt:processor==
==$xslt:processor=={|width='100%'
|-
| width='90120' | '''Signatures'''|{{Code|'''$xslt:processor''' () as xs:string}}<br />
|-
| '''Summary'''
|This variable contains Returns the name of the applied XSLT processor, or the path to a custom implementation (currently: "Java", "Saxon EE", "Saxon PE", or "Saxon HE").<br />
|}
==$xslt:version== {|width='100%'
|-
| width='90120' | '''Signatures'''|{{Code|'''$xslt:version''' () as xs:string}}<br />
|-
| '''Summary'''
|This variable contains Returns the supported XSLT version (currently: "1.0" or "23.0"). "Unknown" is returned if a custom implementation was chosen.<br />
|}
=Functions=xslt:transform==
==xslt:transform=={|width='100%'
|-
| width='90120' | '''Signatures'''|{{Func|xslt:transform|$input as item(), $stylesheet as item()|node()}}<br />{{Func|xslt:transform|$input as item(), $stylesheet as item(), $params as map(*)?|node()}}<br />{{Func|xslt:transform|$input as item(), $stylesheet as item(), $args as map(*)?, $options as map(*)?|node()}}
|-
| '''Summary'''
|Transforms the document specified by {{Code|$input}}, using the XSLT template specified by {{Code|$stylesheet}}, and returns the result as {{Code|node()}} instance. {{Code|$input}} and {{Code|$stylesheet}} can be specified as<br />* {{Code|xs:string}}, containing the path to the documentstylesheet URI,
* {{Code|xs:string}}, containing the document in its string representation, or
* {{Code|node()}}, containing the document itself.
[[Catalog Resolver|XML Catalog files]] will be considered when resolving URIs. Variables can be bound to a stylesheet via {{Code|$args}} (only strings are supported when using XSLT 3.0 and Saxon). The following {{Code|$paramsoptions}} argument can be used to bind variables to a stylesheet. It can be specified asare available:* {{Code|elementcache}}: cache XSLT transformer (xslt:parametersspeeds up repeated transformations, but increases memory consumption)|-| '''Error'''|{{Error|error|#Errors}}an error occurred during the transformation process.|} ==xslt: transform-text== {| width='100%'|-| width='120' | '''Signatures'''|{{CodeFunc|&lt;xslt:parameters/&gt;}} must be used transform-text|$input as root elementitem(), and the parameters are specified $stylesheet as child nodes, with the element name representing the key and the text node representing the valueitem()|xs:string}}<br />{{CodeFunc|&lt;xslt:parameters&gt;transform-text|$input as item(), $stylesheet as item(), $params as map(*)?|xs:string}}<br/>&nbsp;&nbsp;&lt;{{Func|xslt:key1&gt;value1&lt;/xslttransform-text|$input as item(), $stylesheet as item(), $params as map(*)?, $options as map(*)?|xs:key1&gt;<br/>&nbsp;&nbsp;string}}|-| '''Summary'''|Transforms the document specified by {{Code|$input}}, using the XSLT template specified by {{Code|$stylesheet}}, and returns the result as string...<br/>&lt;/xslt:parameters&gt;The semantics of {{Code|$params}} and {{Code|$options}}* is the same as for [[Map Module#xslt:transform|map structurexslt:transform]]: all parameters can be directly represented as key/value pairs:.<br /><code>map |-| '''Error'''|{{ "key1" := "value1", ... Error|error|#Errors}}</code><br/>This variant is more compact, and it facilitates the binding of arbitrary data types. Note that only strings are supported when using Saxon (XSLT 2.0). Next, an error occurred during the map structures are not part of the XQuery language yet, as the standardization is still in progresstransformation process.
|}
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">xslt:transform-text(<dummy/>, 'basic.xslt')</presyntaxhighlight>
'''basic.xslt'''
<pre classsyntaxhighlight lang="brush:xqueryxml">
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/"> <result/> 123</xsl:template>
</xsl:stylesheet>
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">123<result/></presyntaxhighlight>
'''Example 2: XSLT transformation of an input document'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
(: Outputs the result as html. :)
declare option output:method 'html';
</books>
let $style :=
<xsl:stylesheet version='23.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml'/>
<xsl:template match="/">
</xsl:stylesheet>
return xslt:transform($in, $style)</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<html>
<body>
</body>
</html>
</presyntaxhighlight>
'''Example 3: Assigning a variable to an XSLT stylesheet'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $in := <dummy/>
let $style := doc('variable.xsl')
return ( xslt:transform($in, $style, <xslt:parameters><xslt:v>1</xslt:v></xslt:parameters>), xslt:transform($in, $style, map { "v" := 1 }))</presyntaxhighlight>
'''variable.xsltxsl'''<pre classsyntaxhighlight lang="brush:xslt">
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
</xsl:template>
</xsl:stylesheet>
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<v>1</v>
<v>1</v>
</presyntaxhighlight=Errors= {| class="wikitable" width="100%"! width="110"|Code|Description|-|{{Code|error}}| An error occurred during the transformation process.|} =Changelog= ;Version 9.2 * Updated: Support for XML Catalog files added. ;Version 9.0 * Updated: [[#xslt:transform|xslt:transform]], [[#xslt:transform-text|xslt:transform-text]]: {{Code|$options}} argument added.* Updated: error codes updated; errors now use the module namespace ;Version 7.6 * Added: [[#xslt:transform-text|xslt:transform-text]]* Updated: [[#xslt:transform|xslt:transform]] returned error code ;Version 7.3
* Updated: $xslt:processor → [[#xslt:processor|xslt:processor]], $xslt:version → [[Category#xslt:version|xslt:XQueryversion]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu