From charlesreid1

This page describes my installation procedure for LibXML and LibXSLT, along with their Python bindings, in Mac OS X.

While my instructions are based on [1], I strongly recommend NOT using that person's method, since it installs the new versions of LibXML and LibXSLT into the same place where the system's versions of the same libraries are, and in the process blows away some important system-level libraries. Instead, use my procedure, which installs the libraries to a custom user-defined location and leaves the system libraries alone.

Step 1: Build

You can download the latest versions of the libxml and libxslt source code from here: http://xmlsoft.org/sources/

Building libxml

To build libxml, first extract the tarball, and create a script to run configure, specifying an installation location and the location of whatever version of Python you want to use. In this case, I'm using the Python.org 2.7 binary for Mac OS X, which installs a Mac OS X framework.

./configure \
    --with-python=/Library/Frameworks/Python.framework/Versions/2.7 \
    --prefix=/Users/charles/pkg/libxml2/2.7.8 \

Now run the usual

$ make -j2
$ make install

and make this version the standard version of libxml,

$ ln -fs $HOME/pkg/libxml2/2.7.8 $HOME/pkg/libxml2/std

Building libxslt

Repeat the same procedure with the libxslt tarball, and create a configure script for libxslt as well:

./configure \
    --with-python=/Library/Frameworks/Python.framework/Versions/2.7 \
    --prefix=/Users/charles/pkg/libxslt/1.1.26 \
    --with-libxml-prefix=/Users/charles/pkg/libxml2/std \
    --with-libxml-include-prefix=/Users/charles/pkg/libxml2/std/include \
    --with-libxml-libs-prefix=/Users/charles/pkg/libxml2/std/lib \

Once you run the configure script, run the usual

$ make -j2
$ make install

and make this the standard version of of libxslt,

$ ln -fs $HOME/pkg/libxslt/1.1.26 $HOME/pkg/libxslt/std

Python Bindings

If you point configure to the correct version of Python, the Python bindings should be included for free.

You will need to update your $PYTHONPATH variable, though, by adding this to your .profile (or wherever you define $PYTHONPATH):


# libxml 
export PYTHONPATH="${HOME}/pkg/libxml2/std/lib/python2.7/site-packages:${PYTHONPATH}"

# libxslt 
export PYTHONPATH="${HOME}/pkg/libxslt/std/lib/python2.7/site-packages:${PYTHONPATH}"


Test

(Note: this test does not check what version of libxml or libxslt are being used, so just because this test works doesn't mean your installation went as planned.)

You can test libxml/libxslt by running a simple example:

import libxml 
import libxslt

doc = libxml2.parseFile("test.xsl")
stylesheet = libxslt.parseStylesheetDoc(doc)
style.freeStylesheet()


where test.xsl contains some XSL, e.g.:


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

<xsl:template match="/">
<html>
<head>
<title>XML XSL Example</title>
<style type="text/css">
body
{
margin:10px;
background-color:#ccff00;
font-family:verdana,helvetica,sans-serif;
}

.tutorial-name
{
display:block;
font-weight:bold;
}

.tutorial-url
{
display:block;
color:#636363;
font-size:small;
font-style:italic;
}
</style>
</head>
<body>
<h1>This is a page</h1>
<p>With some text</p>
  <xsl:apply-templates/>
</body>
</html>

</xsl:template>

<xsl:template match="tutorial">
  <span class="tutorial-name"><xsl:value-of select="name"/></span>
  <span class="tutorial-url"><xsl:value-of select="url"/></span>
</xsl:template>

</xsl:stylesheet>