Changes

Jump to navigation Jump to search
1,399 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.
=PropertiesFunctions=
==xslt:processor==
{| width='100%'
|-
| width='90120' | '''Signatures'''
|{{Code|'''xslt:processor'''() as xs:string}}<br />
|-
{| width='100%'
|-
| width='90120' | '''Signatures'''
|{{Code|'''xslt:version'''() as xs:string}}<br />
|-
| '''Summary'''
|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, which can either be specified<br />are available:* as children of an {{Code|<xsltcache}}:parameters/>cache XSLT transformer (speeds up repeated transformations, but increases memory consumption)|-| '''Error'''|{{Error|error|#Errors}} element; ean error occurred during the transformation process.g.:<pre class|} =="brushxslt:xml">transform-text==<xslt:parameters> <xslt:key1 value{| width='value1100%'/> ...|-| width='120' | '''Signatures'''|{{Func|xslt:transform-text|$input as item(), $stylesheet as item()|xs:string}}<br />{{Func|xslt:parameters>transform-text|$input as item(), $stylesheet as item(), $params as map(*)?|xs:string}}<br /pre>{{Func|xslt:transform-text|$input as item(), $stylesheet as item(), $params as map(* )?, $options as map, which contains all key/value pairs(*)?|xs:string}}|-<pre class="brush:xml">| '''Summary'''map |Transforms the document specified by {{Code|$input}}, using the XSLT template specified by {{ "key1" := "value1"Code|$stylesheet}}, and returns the result as string.The semantics of {{Code|$params}} and {{Code|$options}} is the same as for [[#xslt:transform|xslt:transform]].. }<br /pre>Note that only strings are supported when using Saxon (XSLT 2.0)|-| '''Error'''|{{Error|error|#Errors}} an error occurred during the transformation process.
|}
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">xslt:transform-text(<dummy/>, 'basic.xslt')</presyntaxhighlight>
'''basic.xslt'''
<pre classsyntaxhighlight lang="brush:xml">
<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
* Updates: $xslt:processor → [[#xslt:processor|xslt:processor]], $xslt:version → [[#xslt:version|xslt:version]]
* Updated: $xslt:processor → [[#xslt:processor|xslt:processor]], $xslt:version → [[Category#xslt:version|xslt:XQueryversion]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu