[xslt] Template content problem with python?



Ok, so I'm playing with the python interface. Here is my
'work-in-progress' xslt.py script (laugh if you must, this is my
second ever Python script :-):

#!/usr/bin/python -u

import sys
import string
import libxml2
import libxslt

# Check the arguments

usage = "Usage: %s xmlfile.xml xslfile.xsl [outputfile] [param1=val [param2=val]...]" % sys.argv[0]

xmlfile = None
xslfile = None
outfile = None
params  = {}

try:
    xmlfile = sys.argv[1]
    xslfile = sys.argv[2]
except IndexError:
    print usage;
    sys.exit(1)

try:
    outfile = sys.argv[3]
    if string.find(outfile, "=") > 0:
        name, value = string.split(outfile, "=", 2);
        params[name] = value

    count = 4;
    while (sys.argv[count]):
        try:
            name, value = string.split(sys.argv[count], "=", 2);
            if params.has_key(name):
                print "Warning: '%s' re-specified; replacing value" % name
            params[name] = value
        except ValueError:
            print "Invalid parameter specification: '" + sys.argv[count] + "'"
            print usage
            sys.exit(1);
        count = count+1;
except IndexError:
    pass

# Memory debug specific
libxml2.debugMemory(1)

nodeName = None

# ======================================================================

def f(ctx, str):
    global nodeName

    #
    # Small check to verify the context is correcly accessed
    #
    try:
        pctxt = libxslt.xpathParserContext(_obj=ctx)
        ctxt = pctxt.context()
        tctxt = ctxt.transformContext()
        nodeName = tctxt.insertNode().name
    except:
        pass

    print "called with str=" + str

    return string.upper(str)

# ======================================================================

libxslt.registerExtModuleFunction("foo", "http://example.com/foo";, f)

styledoc = libxml2.parseFile(xslfile)
style = libxslt.parseStylesheetDoc(styledoc)

doc = libxml2.parseFile(xmlfile)

result = style.applyStylesheet(doc, { "bar": "'success'" })

style.freeStylesheet()
doc.freeDoc()
result.freeDoc()

# Memory debug specific
libxslt.cleanup()
if libxml2.debugMemory(1) != 0:
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
    libxml2.dumpMemory()

With this test document:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
                  "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd";>
<article>
<articleinfo>
<title>Unit Test: table.001</title>
<releaseinfo role="CVS">$Id: table.001.xml,v 1.2 2002/01/18 21:06:45 nwalsh Exp $</releaseinfo>
<author><firstname>Norman</firstname><surname>Walsh</surname>
        <affiliation><address><email>ndw@nwalsh.com</email></address></affiliation>
</author>
</articleinfo>

<table frame="all">
<title>TFoot Test</title>
<tgroup cols="2">
<colspec colwidth="2*"/>
<colspec colwidth="3*"/>
<tfoot>
<row>
<entry>Foot Left</entry>
<entry>Foot Right</entry>
</row>
</tfoot>
<tbody>
<row>
<entry>Body Left</entry>
<entry>Body Right</entry>
</row>
</tbody>
</tgroup>
</table>

</article>

If I run:

$ python xslt.py test.xml http://docbook.sf.net/release/xsl/current/html/docbook.xsl

I get:

compilation error: file /sourceforge/docbook/xsl/html/synop.xsl element text
xsltParseTemplateContent: xslt:text content problem
compilation error: file /sourceforge/docbook/xsl/html/synop.xsl element text
xsltParseTemplateContent: xslt:text content problem
compilation error: file /sourceforge/docbook/xsl/html/synop.xsl element text
xsltParseTemplateContent: xslt:text content problem
....

Running xsltproc directly doesn't produce this problem.

                                        Be seeing you,
                                          norm

-- 
Norman Walsh <ndw@nwalsh.com> | No matter how cynical I get, I find I
http://nwalsh.com/            | just can't keep up.--Lily Tomlin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]