RSS

Tag Archives: xslt

The XSLT series. Season 1 – Episode 2: the GNOME way

Here I’ll try the GNOME implementation of the W3C specification. You’ll find on their site the (main) C implementation as well as many different bindings (as CPP, Perl, Ruby, …).

Windows

Under Windows you can use the Zlatovic build. Here’s all we need to make some test:

input.xml
transform.xsl
iconv.dll
zlib1.dll
libexslt.dll
libxml2.dll
libxslt.dll
xsltproc.exe

The XSLT transformation will be performed this way:

xsltproc --output output.html transform.xsl input.xml

Linux

Under Linux we’ll build the xsltproc tool ourselves. It’s pretty easy:

download libxslt-1.1.28.tar.gz
tar xvf libxslt-1.1.28.tar.gz
cd libxslt-1.1.28
./configure
make
cd xsltproc

Well done! Now we can launch the transform as always:

./xsltproc --output output.html transform.xsl input.xml

Other Info

There’s a very well done tutorial on how to use this library to write your own program on the xmlsoft.org page.

Advertisements
 
Leave a comment

Posted by on 2013/11/27 in dev

 

Tags: ,

The XSLT series. Season 1 – Episode 1: the Xalan way

Advisory: as the http://xml.apache.org site says “2012-04-12 – Project XML has been retired.“. It has been moved into their “Attic“.

Xalan is the name of the Apache implementation of the W3C XSL Transformation (1.0). It is built on Xerces, a processor for parsing, validating, serializing and manipulating XML.
It exists in two flavours: the C++ one and the Java one.

On http://xalan.apache.org/index.html there’s all we need. Let’s play with it!

Here’s a sample (C++ / Windows) that uses a precompiled executable that made all the work. Given the following files:

input.xml
transform.xsl
Xalan-C_1_11.dll
XalanMessages_1_11.dll
xerces-c_3_1.dll
Xalan.exe

we can made our XSLT transform easily this way:

Xalan.exe -o output.html input.xml transform.xsl

That’s all…now let’s check the output.html file to see the result.

Now let’s try the Java version:

input.xml
transform.xsl
serializer.jar
xalan.jar
xercesImpl.jar
xml-apis.jar

Here’s the command:

java -cp "xalan.jar;serializer.jar;xml-apis.jar;xercesImpl.jar" org.apache.xalan.xslt.Process -IN input.xml -XSL transform.xsl -OUT output.html

As you can see here we’re OS agnostic. I also passed the CLASSPATH as a parameter, but we all know that the correct way is to set it as an environment variable.
The result is again into the output.html file.

Finally let’s play coding this libraries:

// Exerpt from http://xml.apache.org/xalan-c/usagepatterns.html#xalantransformer

#include <xalanc/Include/PlatformDefinitions.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>
#include <xalanc/XSLT/XSLTResultTarget.hpp>
#include <xalanc/XSLT/XSLTInputSource.hpp>

int main(int argc, char *argv[])
{
  int theResult = -1;

  // Locally define the namespaces
  XALAN_USING_XERCES(XMLPlatformUtils)
  XALAN_USING_XALAN(XalanTransformer)	

  // Initialize Xerces and Xalan
  XMLPlatformUtils::Initialize();
  XalanTransformer::initialize();
	
  // Create a XalanTransformer
  XalanTransformer theXalanTransformer;

  // Input / output
  XALAN_USING_XALAN(XSLTInputSource)
  XALAN_USING_XALAN(XSLTResultTarget)
  XSLTInputSource xmlIn("input.xml");
  XSLTInputSource xslIn("transform.xsl");
  XSLTResultTarget xmlOut("output.html");

  // Do the trasform
  theResult = theXalanTransformer.transform(xmlIn, xslIn, xmlOut);

  // Shut down and terminate
  XalanTransformer::terminate();
  //XMLPlatformUtils::Terminate(); // mem leak?
  XalanTransformer::ICUCleanUp();
	
  return theResult;
}
 
Leave a comment

Posted by on 2013/10/24 in dev

 

Tags: , ,

The XSLT series. Season 1 – Episode 0 (pilot)

Who knows me knows that I’m not a huge XML fan, but I find XSLT pretty funny. It’s a sort of Philosopher’s Stone: you can take apples and transform them into stones =)

This is the first post of a series: I’ll take some easy XSLT recipe and I’ll work on it with some sample.
I am interested in different XSLT implementations and I’ll explore the most common out there.

Every sample will use the same input xml file (named “input.xml”):

<Root>
	<Cities continent="EU">
		<City id="2">Berlin</City>
		<City id="3">Rome</City>
	</Cities>
	<Cities continent="US">
		<City id="1">New York</City>
	</Cities>
</Root>

and the same XSLT (named “transform.xsl”):

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- :::::::::::::::: MAIN DOCUMENT :::::::::::::::: -->
<xsl:template match="/">
  <html>
  <head>
  <title>Cities</title>
  </head>
  <body>  
    <table>
    <tr>
      <xsl:apply-templates select="/Root/Cities" />
    </tr>
    </table>
  </body>
  </html>
</xsl:template>

<!-- :::::::::::::::: TABLE :::::::::::::::: -->
<xsl:template match="Cities/City">
  <!-- we're on a City node and we call its template -->
  <xsl:call-template name="citieshandler">
    <xsl:with-param name="continent"><xsl:value-of select="../@continent" /></xsl:with-param>
  </xsl:call-template>
</xsl:template>

<!-- :::::::::::::::: TABLE CELL TEMPLATE :::::::::::::::: -->
<xsl:template name="citieshandler">
  <xsl:param name="continent">unknown</xsl:param>

  <!-- write a comment (from the parameter) -->
  <xsl:comment><xsl:value-of select="$continent" /></xsl:comment> 
  <!-- the table row / cell -->
  <tr>
    <td>
      <xsl:value-of select="." />
    </td>
  </tr>
</xsl:template>

</xsl:stylesheet>

This is just the intro…soon I’ll try the implementations from GNOME (libxml2 / libxslt), Apache (xalan / xerces), Saxon, Qt native implementations (QtXml / QtXmlPatterns) and finally C# (XslCompiledTransform xslt / XmlDocument) with one eye at the Mono implementation.

See you soon!

 
Leave a comment

Posted by on 2013/10/21 in dev

 

Tags: ,