Re: [xslt] xsltRegisterExtModule: initFunc not getting called

> > I call xsltRegisterExtModule, passing it the module initialization
> > function (which contains calls to xsltRegisterExtFunction). But that
> > module initialization function never get called

>  unclear, I have a really hard time reading your code.

Sorry, that's probably due to all the error checking code (in C++).
At the end of this mail is a cut down version without error checking.

> This might be a bug in libxslt or in your code, nothing obvious,
> the best is to take a debugger and step through it to see what's
> happening when running the transformation.

Ok. Using a debugger can be my next approach.

However, I'm having second thoughts am am thinking of registering
the external XPath functions using xsltRegisterExtModuleFunction
(instead of using xsltRegisterExtModule and registering the XPath
functions from within the module initialization function).  Mainly
because for the module to be initialized, the stylesheet would
have to use the extension-element-prefixes attribute in the
stylesheet element.  Since I'm only defining external functions,
and not external elements, there is nothing in XSLT per se that
would require the stylesheet writer to use that attribute.



#include <stdio.h>

#include <libxml/xpathInternals.h>
#include <libxslt/xsltconfig.h>
#include <libxslt/extensions.h>
#include <libxslt/transform.h>

static const char* NAMESPACE = "";;

// The implementation of the XPath function

xpath_function_hello (xmlXPathParserContextPtr ctxt, int nargs)
  valuePush(ctxt, xmlXPathNewString(BAD_CAST "Hello world"));

// Module initialization and termination functions

module_init(xsltTransformContextPtr xpath_ctxt,
            const xmlChar* uri)
  printf("DEBUG: yippee! module_init called ************\n");
  xsltRegisterExtFunction(xpath_ctxt, BAD_CAST "hello", uri,
  return NULL;

module_term (xsltTransformContextPtr ctxt,
           	const xmlChar *URI,
		void *data)
  // do nothing


main (int argc, char** argv)
  xsltStylesheet* stylesheet;
  xmlDocPtr input;
  xmlDocPtr result;

#if 0
  /* If this is used, program works, but module_init still not called */
  xsltRegisterExtModuleFunction(BAD_CAST "hello", BAD_CAST NAMESPACE,

  if (argc != 3) {
    printf("Usage: %s xsltfile xmlfile\n", argv[0]);
    return 1;

  xsltRegisterExtModule(BAD_CAST NAMESPACE, module_init, module_term);

  stylesheet = xsltParseStylesheetFile(BAD_CAST argv[1]);
  input = xmlParseFile(argv[2]);

  result = xsltApplyStylesheet(stylesheet, input, NULL);

  xmlSaveFormatFile ("-", result, 1 /* indent */);

  return 0;


Testing XSLT script:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="";

<xsl:template match="story">
      <h1><xsl:value-of select="TEST:hello()"/></h1>


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