libxslt TutorialThis tutorial is based on the Libxslt Tutorial by John Fleck ( ©2001 ), converted for BlitzMax by Bruce Henderson
Table of Contents
A tutorial on building a simple application using the libxslt library to perform XSLT transformations to convert an XML file into HTML.
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:
Table of Contents
To transform an XML file, you must perform three functions:
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.
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.
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.
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.
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)
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.
After you are finished, libxslt and libxml provide functions for deallocating memory.
cur.free()
res.free()
doc.free()
xsltCleanupGlobals()
xmlCleanupParse()
Free the memory used by your stylesheet.
Free the memory used by the results document.
Free the memory used by your original document.
Free memory used by libxslt global variables
Free memory used by the XML parser
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()