Changes

Jump to navigation Jump to search
1,038 bytes added ,  06:46, 29 November 2019
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 [http://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:
<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);
=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=
{| 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 />
|}
==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 class="brush:xquery">
xslt:transform-text(<dummy/>, 'basic.xslt')
</pre>
<pre class="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>
</pre>
'''Result:'''
<pre class="brush:xml"><result/>123</pre>
'''Example 2: XSLT transformation of an input document'''
</books>
let $style :=
<xsl:stylesheet version='23.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml'/>
<xsl:template match="/">
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 }))
</pre>
'''variable.xsltxsl'''
<pre class="brush:xslt">
<xsl:stylesheet version='1.0'
=Errors=
{| width='100%' class="wikitable" width="100%"! width="5%110"|Code! width="95%"|Description
|-
|{{Code|BXVA0001error}}| An error occurred during the transformationprocess.
|}
=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.5.16 * 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 → [[#xslt:version|xslt:version]]
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu