Using EXSLT extension objects with Ibex .NET
Using the .Net version of Ibex PDF Creator you can extend the functionality of the
.NET XSLT processor.
In this example we use the EXSLT extensions, the home
page for which is
http://exslt.org/.
The EXSLT extensions provide functions such as date and time handling
which can be invoked as part of XSLT translation.
This example shows how to link the EXSLT extensions with your application and invoke
its functions from an XSL stylesheet.
Approach
EXSLT ships as a .NET assembly. We need to:
- link this assembly to our application
- tell Ibex the namespace used for EXSLT
- pass Ibex an object which implements the ESXLT extensions
- modify our stylesheet to call the EXSLT functions.
Downloading EXSLT
The version of EXSLT used in this example is called
Mvp.Xml.Dll and was downloaded from
http://www.codeplex.com/MVPXML. Additional information can be found at
http://msdn2.microsoft.com/en-us/library/aa468553.aspx
Linking EXSLT with the application
In this example we use a simple command line program called ExsltExample.cs, which calls Ibex like this:
using System;
using System.IO;
using System.Collections;
using ibex4;
using Mvp.Xml.Exslt;
public class test {
static void Main( string[] args ) {
FODocument doc = new FODocument();
FileStream xml = new FileStream( "exslt.xml", FileMode.Open, FileAccess.Read );
FileStream xsl = new FileStream( "exslt.xsl", FileMode.Open, FileAccess.Read );
FileStream pdf = new FileStream( "exslt.pdf", FileMode.Create, FileAccess.Write );
Hashtable xslArgs = new Hashtable();
xslArgs.Add( "http://exslt.org/dates-and-times", new ExsltDatesAndTimes() );
doc.generate( xml, xsl, pdf, true, xslArgs );
}
}
This program is compiled from the command line and linked with EXSLT using the command:
csc /r:ibex20.dll /r:Mvp.Xml.dll ExsltExample.cs
This program does a number of things to support using EXSLT:
-
we reference the EXSLT namespace with using Mvp.Xml.Exslt;
-
we tell Ibex about EXSLT by creating a Hashtable of arguments for the XSLT process,
adding an instance of the ExsltDatesAndTimes object to that hashtable, and passing
the hashtable to the Ibex generate() method.
Invoking EXSLT in the stylesheet
The data used in this simple example looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<forecast>
<city name="Wellington" temp="20"/>
<city name="Auckland" temp="5"/>
<city name="London" temp="22"/>
</forecast>
The stylesheet used to transform this data is:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/XSL/Format"
xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:strip-space elements="*"/>
<xsl:template match="forecast">
<root>
<layout-master-set>
<simple-page-master master-name="page-layout">
<region-body margin="2.5cm" region-name="body"/>
</simple-page-master>
</layout-master-set>
<page-sequence master-reference="page-layout">
<flow flow-name="body">
<block>
Printed on <xsl:value-of select="date:date()"/>
</block>
<xsl:apply-templates select="city"/>
</flow>
</page-sequence>
</root>
</xsl:template>
<xsl:template match="city">
<block>
<xsl:value-of select="@name"/>
</block>
</xsl:template>
</xsl:stylesheet>
In this stylesheet we need to tell the .NET XSLT processor about the EXSLT namespace. This is done
by adding the namespace declaration to the xsl:stylesheet element like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/XSL/Format"
xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format"
xmlns:date="http://exslt.org/dates-and-times">
We can then invoke the functions of the ESXLT object like this:
Printed on <xsl:value-of select="date:date()"/>
Downloads
Downloads for this example are:
The XML file
exslt.xml.
The XSL file
exslt.xsl.