Re: [xslt] python xslt messages



On Fri, 12 Apr 2002, Daniel Veillard wrote:

> On Fri, Apr 12, 2002 at 06:46:13AM -0400, Sam Brauer wrote:
> > I've been happily using the Python libxslt interface for a few weeks 
> > now, but I have noticed that sometimes there are error messsages written 
> > to stderr (for example, when a variable is referred to but was never 
> > declared, or when an xpath expression cannot be evaluated).  However no 
> > exceptions are thrown and I get a results back from 
> > parseStylesheetDoc() and applyStylesheet(), as if everything succeeded.
> > 
> > Is there some way via Python to access the warning/error messages (other 
> > than redirecting stderr) ?
> 
>   Have you tried 
>     libxml2.registerErrorHandler(your_function, your_context) ?
> 
> Generating exceptions would need to build a bit more Python glue around
> the existing wrappers to check the error code and raise an exception 
> if detected.
> 
> Daniel
> 
> 

I experimented with this, but its behavior seems a little weird (to me 
anyway).  I'm including a short example script that uses 
libxml2.registerErrorHandler.  If I run it in a case where either the 
stylesheet or document file is missing (which is the case tested by 
error.py that comes with the package), then it works great.
It also works great if both files exist but one is not well-formed.


But consider these cases:

Case 1. The stylesheet is well-formed, but contains an invalid xsl tag, 
such as <xsl:foo/>

stderr:
compilation error: file test.xsl element foo
xsltStylePreCompute: unknown xsl:foo
compilation error: file test.xsl element foo
xsltParseStylesheetTop: ignoring unknown foo element

error handler:
None   (error handler wasn't called)


Case 2. The stylesheet is well-formed, but refers to an undeclared 
variable.

stderr:
runtime error: file test.xsl element for-each
unregistered variable foo

error handler:
xmlXPathCompiledEval: evaluation failed



In both of these cases, messages are going to stderr that I would like to 
be able to collect with a callback.  Does the API currently provide some 
way to do this?

By the way, I'm sorry to nag you with this.  
I'm very impressed with libxml2/libxslt and very grateful for your work!  

-- 
Sam Brauer
Systems Programmer
sam@webslingerZ.com

import libxml2
import libxslt
import sys

if(len(sys.argv) < 3):
	sys.stderr.write("usage: %s stylesheet document\n" % sys.argv[0])
	sys.exit(1)

xslfn = sys.argv[1]
xmlfn = sys.argv[2]

error_msg = ""

def libxsltErrorCallback(ctx, str):
	global error_msg
	error_msg += "%s %s" % (ctx, str)

libxml2.registerErrorHandler(libxsltErrorCallback, "")

styledoc = None
style = None
doc = None
result = None
output = None

try:
	doc = libxml2.parseFile(xmlfn)
	styledoc = libxml2.parseFile(xslfn)
	style = libxslt.parseStylesheetDoc(styledoc)
	result = style.applyStylesheet(doc, None)
	output = result.serialize()
except:
	pass

if(result): result.freeDoc()
if(doc): doc.freeDoc()
if(style): style.freeStylesheet()
elif(styledoc): styledoc.freeDoc()

if(error_msg): print "ERROR:\n" + error_msg
else: print "SUCCESS:\n" + output





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