libxslt Tutorial

This tutorial is based on the Libxslt Tutorial by John Fleck ( ©2001 ), converted for BlitzMax by Bruce Henderson

Table of Contents

Abstract

A tutorial on building a simple application using the libxslt library to perform XSLT transformations to convert an XML file into HTML.

Introduction

The Extensible Markup Language (XML) is a World Wide Web Consortium standard for the exchange of structured data in text form. Its popularity stems from its universality. Any computer can read a text file. With the proper tools, any computer can read any other computer's XML files.

One of the most important of those tools is XSLT: Extensible Stylesheet Language Transformations. XSLT is a declarative language that allows you to translate your XML into arbitrary text output using a stylesheet. libxslt provides the functions to perform the transformation.

This tutorial illustrates a simple program that reads an XML file, applies a stylesheet and saves the resulting output.

References:

Primary Functions

Table of Contents

To transform an XML file, you must perform three functions:

  1. parse the input file
  2. parse the stylesheet
  3. apply the stylesheet

Preparing to Parse

Before you can begin parsing input files or stylesheets, there are several steps you need to take to set up entity handling. These steps are not unique to libxslt. Any libxml program that parses XML files would need to take similar steps.

First, you need set up some libxml housekeeping. Pass the integer value 1 to the xmlSubstituteEntitiesDefault function, which tells the libxml parser to substitute entities as it parses your file. (Passing 0 causes libxml to not perform entity substitution.)

Second, set xmlLoadExtDtdDefaultValue equal to 1. This tells libxml to load external entity subsets. If you do not do this and your input file includes entities through external subsets, you will get errors.

Parse the Stylesheet

Parsing the stylesheet takes a single function call, which takes a string variable:

cur = TxsltStylesheet.parseStylesheetFile("example_sort.xslt")
The return value is of type TxsltStylesheet, a Type that contains the stylesheet tree and other information about the stylesheet. It can be manipulated directly, but for this example you will not need to.

Parse the Input File

Parsing the input file takes a single function call:

doc = TxmlDoc.parseFile("example_sort.xml")
It returns a TxmlDoc, an xml type that contains the document tree. It can be manipulated directly, but for this example you will not need to.

Applying the Stylesheet

Now that you have trees representing the document and the stylesheet in memory, apply the stylesheet to the document. The method that does this is applyStylesheet:

res = cur.applyStylesheet(doc)
The method takes a TxmlDoc, the value returned by the previous function.

Saving the result

Libxslt includes methods for saving the resulting output. For this example, saveResultToFilename is used, and the results are saved to the file "example_sort.html":

cur.saveResultToFilename("example_sort.html", res)

Note

Libxml also contains output functions, such as saveFile, which can be used here. However, output-related information contained in the stylesheet, such as a declaration of the encoding to be used, will be lost if one of the libxslt save functions is not used.

Cleanup

After you are finished, libxslt and libxml provide functions for deallocating memory.

1cur.free()
2res.free()
3doc.free()

4xsltCleanupGlobals()
5xmlCleanupParse()

1 Free the memory used by your stylesheet.

2 Free the memory used by the results document.

3 Free the memory used by your original document.

4 Free memory used by libxslt global variables

5 Free memory used by the XML parser

 

A. The Code

You can open the source here.

SuperStrict

Framework BaH.libxslt
Import BaH.libxml

Local cur:TxsltStylesheet
Local doc:TxmlDoc
Local res:TxmlDoc

xmlSubstituteEntitiesDefault(1)
xmlLoadExtDtdDefaultValue = 1

cur = TxsltStylesheet.parseStylesheetFile("example_sort.xslt")

doc = TxmlDoc.parseFile("example_sort.xml")
res = cur.applyStylesheet(doc)

cur.saveResultToFilename("example_sort.html", res)

cur.free()
res.free()
doc.free()

xsltCleanupGlobals()
xmlCleanupParser()