[xslt] xsltRegisterExtModule: initFunc not getting called



G'day,

I'm trying to implement my own XPath extension functions in libXSLT for
use in XSLT, but can't register my module properly.

I call xsltRegisterExtModule, passing it the module initialization
function (which contains calls to xsltRegisterExtFunction). But that
module initialization function never get called (and consequently, the
functions don't get registered and I get an "unregistered function"
error when the XSLT script is applied).

I've read the "Writing extensions" guide, generated API docs, and had a
quick browse through the source code, but couldn't figure out what I'm
doing wrong.  Any help would be appreciated.  Thanks.

Hoylen



This is the XSLT input file I'm using: the extension is referenced in
the "extension-element-prefixes" attribute. The name of the extension
function is "hw".

<?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:hw()"/></h1>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>
</xsl:stylesheet>


And the rest of this e-mail is the C++ test program I'm using.
It simply calls:
1. xsltRegisterExtModule
2. xsltParseStylesheetFile for the stylesheet (above)
3. xmlParseFile for the input XML
4. xsltApplyStylesheet
but the init function passed to xsltRegisterExtModule never gets
invoked.

//----------------------------------------------------------------
#include <iostream>

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

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

//----------------------------------------------------------------

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

//----------------------------------------------------------------

void*
tb_module_init(xsltTransformContextPtr xpath_ctxt,
	       const xmlChar* uri) {

  std::cerr << "DEBUG: tb_module_init called\n";

  if (xsltRegisterExtFunction(xpath_ctxt, BAD_CAST "hw", uri,
			      function_hw) != 0) {
    std::cerr << "Error: could not register function" << std::endl;
  }

  return NULL;
}

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

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

int
main (int argc, char** argv) {

  std::cout << "libXSLT version: " << xsltEngineVersion << std::endl;
  std::cout << "libXSLT compiled against: " << xsltLibxsltVersion << std::endl;
  std::cout << "libXML compiled against: " << xsltLibxmlVersion << std::endl;

  if (argc != 3) {
    std::cerr << "Usage: " << argv[0] << " xsltfile xmlfile" << std::endl;
    return 1;
  }

  // Register the extension module

  if (xsltRegisterExtModule(BAD_CAST NS, tb_module_init, tb_module_term)) {
    std::cerr << "Error: could not register functions" << std::endl;
    return 1;
  }

  // Get stylesheet

  xsltStylesheet* stylesheet = xsltParseStylesheetFile(BAD_CAST argv[1]);
  if (! stylesheet) {
    std::cerr << "Error: could not load stylesheet: " << argv[1] << std::endl;
    return 1;
  }

  // Get input

  xmlDocPtr input = xmlParseFile(argv[2]);
  if (input == NULL ) {
    xmlFreeDoc(input);
    std::cerr << "Error: loading of input document failed" << std::endl;
    return 1;
  }

  // Apply stylesheet transformation

  const char** params = 0;
  xmlDocPtr result = xsltApplyStylesheet(stylesheet, input, params);
  if (! result) {
    xmlFreeDoc(input);
    std::cerr << "Error: XSLT failed: " << std::endl;
    return 1;
  }

  // Output result

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

  // Finish up

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

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

-- 
________________________________________________________________
hoylen@hoylen.com                         http://www.hoylen.com/




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