<!DOCTYPE refentry SYSTEM "tcldoc-1.0.dtd">

<refentry xmlns:xlink='http://www.w3.org/XLink'>

  <docinfo>
    <author xlink:href='http://www.zveno.com/staff/Steve.Ball/'>
      <firstname>Steve</firstname>
      <surname>Ball</surname>
    </author>
    <copyright>
      <year>2002</year>
      <holder xlink:href='http://www.zveno.com/'>Zveno Pty Ltd</holder>
      <legalnotice>
        <para><!-- insert std Zveno legal notice --></para>
      </legalnotice>
    </copyright>
  </docinfo>

  <refmeta>
    <refentrytitle>TclXSLT</refentrytitle>
    <manvolnum>n</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>::xslt::compile</refname>
    <refname>::xslt::extension</refname>
  </refnamediv>

  <refsynopsisdiv>
    <tclcmdsynopsis>
      <command>package require xslt</command>
    </tclcmdsynopsis>

    <tclpkgsynopsis>
      <package>xslt</package>
      <version>2.5</version>
    </tclpkgsynopsis>

    <tclnamespacesynopsis>
      <tclnamespace>::xslt</tclnamespace>
    </tclnamespacesynopsis>

    <tclcmdsynopsis>
      <command>::xslt::compile</command>
      <arg><replaceable>doc</replaceable></arg>
    </tclcmdsynopsis>

    <tclcmdsynopsis>
      <command><replaceable>ssheet</replaceable></command>
      <arg><replaceable>method</replaceable></arg>
      <group choice='opt' rep='repeat'><arg>option</arg><arg>value</arg></group>
      <group choice='opt' rep='repeat'><arg>args</arg></group>
    </tclcmdsynopsis>

    <tclcmdsynopsis>
      <command>::xslt::extension</command>
      <methodname>add</methodname>
      <arg><replaceable>nsuri</replaceable></arg>
      <arg><replaceable>tcl-namespace</replaceable></arg>
    </tclcmdsynopsis>
  </refsynopsisdiv>

  <refsect1>
    <title>Description</title>

    <para>TclXSLT is a wrapper for the <ulink url='http://xmlsoft.org/'>Gnome libxslt library</ulink> that allows an application to perform XSL transformations (XSLT).  The package also provides a binding to the XSLT extension mechanism so that XSLT extension may be implemented using Tcl scripts.</para>

    <para>Transformation only works with documents created by TclDOM/libxml2.</para>

    <para>The TclXSLT package makes extensive use of Tcl objects.  Compiled XSL stylesheets are stored as the internal representation of a Tcl object.  Source and result documents are accessed via TclDOM's C interface as Tcl objects.  This allows the application to cache parsed XML documents and compiled XSL stylesheets for better runtime performance.</para>
  </refsect1>

  <refsect1>
    <title>Packages and Namespaces</title>

    <para>The TclXSLT package defines the <tclpackage>xslt</tclpackage> package and also a Tcl namespace using that name.</para>
  </refsect1>

  <refsect1>
    <title>Commands</title>

    <refsect2>
      <title><tclcommand>::xslt::compile</tclcommand></title>

      <para>The <tclcommand>::xslt::compile</tclcommand> command pre-compiles a stylesheet document.  It returns a compiled stylesheet object and also defines a Tcl command to access the stylesheet.  This Tcl command may be used to transform XML documents.</para>

      <example>
        <title>Example</title>
	<programlisting>
set source_doc [::dom::libxml2::parse $XML]
set ssheet_doc [::dom::libxml2::parse $XSLstylesheet]
set ssheet [::xslt::compile $ssheet_doc]
set result [$ssheet transform $source_doc]
</programlisting>
      </example>

      <para>NB.  It is advisable to use the <option>-baseuri</option> option when parsing the source and stylesheet documents to allow external resources to be resolved.</para>
    </refsect2>

    <refsect2>
      <title>Stylesheet Command</title>

      <para>The stylesheet command created by <tclcommand>::xslt::compile</tclcommand> command accesses a compiled stylesheet.</para>

      <refsect3>
        <title>Command Methods</title>

	<para>The following command methods may be used:</para>

	<variablelist>
	  <varlistentry>
	    <term><arg>cget</arg> <arg><replaceable>option</replaceable></arg></term>
	    <listitem>
	      <para>Returns the value of an option.  See below for the list of valid options.</para>
	    </listitem>
	  </varlistentry>
	  <varlistentry>
	    <term><arg>configure</arg> <arg><replaceable>option</replaceable> <replaceable>value</replaceable></arg></term>
	    <listitem>
	      <para>Sets the value of an option.  Available options are as follows:</para>
	      <varlistentry>
	        <varlistentry>
		  <term><option>-messagecommand</option> <arg><replaceable>script</replaceable></arg></term>
		  <para>This option specifies a Tcl command to be evaluated when a message is produced by the stylesheet.  Messages may be produced when the stylesheet detects an error during processing, or when the stylesheet uses the <sgmltag>xsl:message</sgmltag> element.</para>

		  <para>It is currently not possible to distinguish between an error message and a message produced using <sgmltag>xsl:message</sgmltag>.</para>
		</varlistentry>
	        <varlistentry>
		  <term><option>-method</option></term>
		  <para>Specifies the output being produced by the stylesheet.  This is usually set by the styesheet's <sgmltag>xsl:output</sgmltag> element.  May have the value <literal>xml</literal>, <literal>html</literal>, <literal>xhtml</literal> or <literal>text</literal>.</para>
		  <para>This is a read-only option.</para>
		</varlistentry>
	      </varlistentry>
	    </listitem>
	  </varlistentry>
	  <varlistentry>
	    <term><arg>transform</arg> <arg><replaceable>source</replaceable></arg> <group opt='repeat'><arg><replaceable>name</replaceable></arg> <arg><replaceable>value</replaceable></arg></group></term>
	    <listitem>
	      <para>Performs an XSL transformation on the given source document.  Stylesheet parameters may be specified as name-value pairs.  The return result is the DOM token for the result document.</para>
	    </listitem>
	  </varlistentry>
	</variablelist>
      </refsect3>

      <refsect3>
        <title>Stylesheet Parameters</title>

	<para>Any number of name-value pairs may be specified as arguments to the <tclcommand><replaceable>stylesheet</replaceable> transform</tclcommand> method.  These are passed as values for parameters in the stylesheet.  <application>libxslt</application> interprets the values as XPath expressions, where the context node is the root node for the source document.  To pass a value as a string it must be XPath-quoted, for example</para>

	<informalexample>
	  <programlisting>
set library "Gnome libxslt"
$ssheet transform $source_doc \
	library '$library' \
	author "'Daniel Veillard'" \
	node {/*/Element[3]}
</programlisting>
	</informalexample>
      </refsect3>

      <para>Following is an example of how to use the <tclcommand><replaceable>stylesheet</replaceable> transform</tclcommand> method.</para>

      <example>
        <title>Example</title>
	<programlisting>
set source_doc [::dom::libxml2::parse $XML]
set ssheet_doc [::dom::libxml2::parse $XSLstylesheet]
set ssheet [::xslt::compile $ssheet_doc]
set result_doc [$ssheet transform $source_doc]
set result_xml [::dom::libxml2::serialize $result_doc \
    -method [$ssheet cget -method]]
</programlisting>
      </example>
    </refsect2>

    <refsect2>
      <title><tclcommand>::xslt::extension</tclcommand></title>

      <para>The <tclcommand>::xslt::extension</tclcommand> command is used to manage extensions of the <application>libxslt</application> library.  The <methodname>add</methodname> is used to register an extension.  The <methodname>remove</methodname> is used to unregister an extension.  See <link>EXTENSIONS</link> for more detail.</para>
    </refsect2>
  </refsect1>

  <refsect1>
    <title>Extensions</title>

    <para>The TclXSLT package allows an application to bind Tcl scripts to the extension mechanism of <application>libxslt</application>.  This means that Tcl scripts may provide the implementation of an XSLT extension element or function.  The binding is achieved to associating a Tcl namespace with an XML namespace.</para>

    <refsect2>
      <title>Implementing An Extension</title>

      <para>The Tcl application uses the <tclcommand>::xslt::extension add</tclcommand> command to register an extension.  An XML Namespace for the extension is specified as an argument, along with a Tcl namespace that will provide implementations of extension elements and functions.  For example,</para>

      <informalexample>
        <programlisting>
::xslt::extension add http://www.zveno.com/Example ::example
</programlisting>
      </informalexample>

      <para>Everytime the <tclcommand>::xslt::transform</tclcommand> command is executed, a newly-created XSLT engine is initialized.  For each registered extension, every procedure in the associated Tcl namespace is defined in the XSLT engine as either an extension element or an extension function.  The procedure is defined as an extension function if it has a variable argument list, otherwise it is defined as an extension element.  The procedure name is used as the local part of the extension name.  For example,</para>

      <informalexample>
        <programlisting>
namespace eval example {
    namespace export myfunc myelement
}

proc example::myfunc {name args} {
    global app
    return $app($name)
}
proc example::myelement {node} {
    global app
    return $app([dom::libxml2::node cget $node -nodeName])
}
</programlisting>
      </informalexample>

      <para>"myfunc" is defined as an extension function and "myelement" is defined as an extension element.</para>

      <refsect3>
        <title>Extension Functions</title>

	<para>The arguments to an extension function are converted to a string value and then passed as parameters to the Tcl procedure.</para>

	<para>The return result of the Tcl procedure becomes the return value of the extension function.  The type of the result is preserved where possible, otherwise it is converted to a string value.</para>
      </refsect3>

      <refsect3>
        <title>Extension Elements</title>

	<para>Extension elements have not been implemented in TclXSLT v1.1.</para>
      </refsect3>
    </refsect2>

    <refsect2>
      <title>Using An Extension</title>

      <para>To invoke an extension in an XSL stylesheet, use the normal XSLT extension mechanism.  The XML Namespace matches the extension to the registered Tcl namespace (NB. the stylesheet author is free to choose any prefix for the extension namespace).  For example,</para>

      <informalexample>
        <programlisting><![CDATA[
<xsl:stylesheet version='1.0'
	xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
	xmlns:eg='http://www.zveno.com/Example'>

  <xsl:template match='/'>
    <xsl:text>Result of calling extension is "</xsl:text>
    <xsl:value-of select='eg:myfunc("foo")'/>
    <xsl:text>".
</xsl:text>
  </xsl:template>

</xsl:stylesheet>
]]></programlisting>
      </informalexample>

      <para>This stylesheet would result in the following Tcl script being evaluated:</para>

      <informalexample>
        <programlisting>
::example::myfunc foo
</programlisting>
      </informalexample>
    </refsect2>
  </refsect1>
</refentry>
