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.

Thanks.

Hoylen




#include <stdio.h>

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

static const char* NAMESPACE = "http://www.example.com/test";;

//----------------------------------------------------------------
// The implementation of the XPath function

void
xpath_function_hello (xmlXPathParserContextPtr ctxt, int nargs)
{
  CHECK_ARITY(0);
  valuePush(ctxt, xmlXPathNewString(BAD_CAST "Hello world"));
}

//----------------------------------------------------------------
// Module initialization and termination functions

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

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

//================================================================

int
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,
				xpath_function_hello);
#endif

  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 */);

  xmlFreeDoc(input);
  xsltFreeStylesheet(stylesheet);
  return 0;
}

//----------------------------------------------------------------
//EOF


Testing XSLT script:

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
		xmlns:TEST="http://www.example.com/test";
		extension-element-prefixes="TEST"
		version="1.0">

<xsl:template match="story">
  <html>
    <head><title>Test</title></head>
    <body>
      <h1><xsl:value-of select="TEST:hello()"/></h1>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>






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