[xml-bindings]Composing documents using Python and libxslt
- From: Jørgen Frøjk Kjærsgaard <jfk metation com>
- To: xml-bindings gnome org
- Subject: [xml-bindings]Composing documents using Python and libxslt
- Date: Thu, 4 Apr 2002 16:12:25 +0200
Hi,
I need to be able to compose XML documents from Python-generated
sub-ducuments. To do this I register a Python extension function using
xsltRegisterExtModuleFunction().
However, I am unable to create an include function, which can be used in my
XML documents. Two things go wrong for some reason:
1) When the argument to the include function itself is an XPath expression,
it is passed to my Python program as a list of C objects (case 1,2 in the
test document below), whereas it is passed as a string if I write a string
directly (case 3).
2) When I attempt to apply-templates to the result of my external Python
function, I get no output.
I have previously succeeded in doing this using Sablotron, however Sablotron
seems to have some serious memory management bugs, which makes it crash all
the time when running under apache and mod_python. libxml/libxslt seems to be
a much stronger toolcase...
** The function:
def _include(self, ctx, str):
print "- CALLING INCLUDE(%s:%s)" % (str[0], type(str))
return "<pre>python pre</pre>"
** The stylesheet:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mtr="http://metation.com/mtr"
exclude-result-prefixes="mtr"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="test">
<html>
test tag start
<xsl:apply-templates/>
test tag slut
</html>
</xsl:template>
<xsl:template match="include">
INCLUDE:<xsl:apply-templates select="mtr:include(@resource)"/>:INCLUDE
</xsl:template>
<xsl:template match="include-raw">
INCLUDE-RAW:<xsl:value-of select="mtr:include(@resource)"/>:INCLUDE-RAW
</xsl:template>
<xsl:template match="incl-direct">
INCL-DIRECT:<xsl:value-of select="mtr:include('DIRECT')"/>:INCL-DIRECT
</xsl:template>
<xsl:template match="pre">
<PRE>
<xsl:value-of select="."/>
</PRE>
</xsl:template>
</xsl:stylesheet>
** The test document:
<test>
<include resource="TEST"/> <!-- case 1 -->
<include-raw resource="RAWTEST"/> <!-- case 2 -->
<incl-direct resource="DIRTEST"/> <!-- case 3 -->
<pre>direkte pre</pre>
</test>
** The result:
- CALLING INCLUDE(<PyCObject object at 0x81edcf0>:<type 'list'>)
- CALLING INCLUDE(<PyCObject object at 0x81edd18>:<type 'list'>)
- CALLING INCLUDE(D:<type 'string'>)
<html>
test tag start
INCLUDE::INCLUDE
INCLUDE-RAW:<pre>python pre</pre>:INCLUDE-RAW
INCL-DIRECT:<pre>python pre</pre>:INCL-DIRECT
<PRE>direkte pre</PRE>
test tag slut
</html>
Thanks, jfk
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]