15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define IN_LIBEXSLT
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "libexslt/libexslt.h"
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(WIN32) && !defined (__CYGWIN__) && (!__MINGW32__)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <win32config.h>
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "config.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxml/tree.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxml/xpath.h>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxml/xpathInternals.h>
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxml/parser.h>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxml/hash.h>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxslt/xsltconfig.h>
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxslt/xsltutils.h>
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxslt/xsltInternals.h>
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <libxslt/extensions.h>
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "exslt.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonInit:
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt: an XSLT transformation context
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @URI: the namespace URI for the extension
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Initializes the SAXON module.
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Returns the data for this transformation
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static xmlHashTablePtr
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonInit (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		const xmlChar *URI ATTRIBUTE_UNUSED) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return xmlHashCreate(1);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonShutdown:
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt: an XSLT transformation context
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @URI: the namespace URI for the extension
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @data: the module data to free up
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Shutdown the SAXON extension module
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonShutdown (xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    const xmlChar *URI ATTRIBUTE_UNUSED,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    xmlHashTablePtr data) {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlHashFree(data, (xmlHashDeallocator) xmlXPathFreeCompExpr);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonExpressionFunction:
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt: an XPath parser context
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @nargs: the number of arguments
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The supplied string must contain an XPath expression. The result of
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the function is a stored expression, which may be supplied as an
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * argument to other extension functions such as saxon:eval(),
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * saxon:sum() and saxon:distinct(). The result of the expression will
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * usually depend on the current node. The expression may contain
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * references to variables that are in scope at the point where
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * saxon:expression() is called: these variables will be replaced in
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the stored expression with the values they take at the time
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * saxon:expression() is called, not the values of the variables at
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the time the stored expression is evaluated.  Similarly, if the
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * expression contains namespace prefixes, these are interpreted in
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * terms of the namespace declarations in scope at the point where the
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * saxon:expression() function is called, not those in scope where the
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * stored expression is evaluated.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * TODO: current implementation doesn't conform to SAXON behaviour
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * regarding context and namespaces.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonExpressionFunction (xmlXPathParserContextPtr ctxt, int nargs) {
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlChar *arg;
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlXPathCompExprPtr ret;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlHashTablePtr hash;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xsltTransformContextPtr tctxt = xsltXPathGetTransformContext(ctxt);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (nargs != 1) {
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xmlXPathSetArityError(ctxt);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    arg = xmlXPathPopString(ctxt);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (xmlXPathCheckError(ctxt) || (arg == NULL)) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xmlXPathSetTypeError(ctxt);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    hash = (xmlHashTablePtr) xsltGetExtData(tctxt,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)					    ctxt->context->functionURI);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ret = xmlHashLookup(hash, arg);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (ret == NULL) {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 ret = xmlXPathCompile(arg);
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 if (ret == NULL) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	      xmlFree(arg);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	      xsltGenericError(xsltGenericErrorContext,
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			"{%s}:%s: argument is not an XPath expression\n",
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			ctxt->context->functionURI, ctxt->context->function);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	      return;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 }
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	 xmlHashAddEntry(hash, arg, (void *) ret);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlFree(arg);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlXPathReturnExternal(ctxt, ret);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonEvalFunction:
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt:  an XPath parser context
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @nargs:  number of arguments
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Implements de SAXON eval() function:
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *    object saxon:eval (saxon:stored-expression)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Returns the result of evaluating the supplied stored expression.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * A stored expression may be obtained as the result of calling
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the saxon:expression() function.
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The stored expression is evaluated in the current context, that
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * is, the context node is the current node, and the context position
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * and context size are the same as the result of calling position()
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * or last() respectively.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonEvalFunction (xmlXPathParserContextPtr ctxt, int nargs) {
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xmlXPathCompExprPtr expr;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xmlXPathObjectPtr ret;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     if (nargs != 1) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  xmlXPathSetArityError(ctxt);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  return;
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     }
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     if (!xmlXPathStackIsExternal(ctxt)) {
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  xmlXPathSetTypeError(ctxt);
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  return;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     }
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     expr = (xmlXPathCompExprPtr) xmlXPathPopExternal(ctxt);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     ret = xmlXPathCompiledEval(expr, ctxt->context);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     valuePush(ctxt, ret);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonEvaluateFunction:
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt:  an XPath parser context
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @nargs: number of arguments
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Implements the SAXON evaluate() function
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     object saxon:evaluate (string)
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The supplied string must contain an XPath expression. The result of
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the function is the result of evaluating the XPath expression. This
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * is useful where an expression needs to be constructed at run-time or
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * passed to the stylesheet as a parameter, for example where the sort
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * key is determined dynamically. The context for the expression (e.g.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * which variables and namespaces are available) is exactly the same as
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * if the expression were written explicitly at this point in the
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * stylesheet. The function saxon:evaluate(string) is shorthand for
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * saxon:eval(saxon:expression(string)).
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonEvaluateFunction (xmlXPathParserContextPtr ctxt, int nargs) {
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     if (nargs != 1) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  xmlXPathSetArityError(ctxt);
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	  return;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     }
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     exsltSaxonExpressionFunction(ctxt, 1);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     exsltSaxonEvalFunction(ctxt, 1);
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonLineNumberFunction:
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @ctxt:  an XPath parser context
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @nargs: number of arguments
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Implements the SAXON line-number() function
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     integer saxon:line-number()
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This returns the line number of the context node in the source document
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * within the entity that contains it. There are no arguments. If line numbers
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * are not maintained for the current document, the function returns -1. (To
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ensure that line numbers are maintained, use the -l option on the command
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * line)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The extension has been extended to have the following form:
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *     integer saxon:line-number([node-set-1])
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * If a node-set is given, this extension will return the line number of the
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * node in the argument node-set that is first in document order.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonLineNumberFunction(xmlXPathParserContextPtr ctxt, int nargs) {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xmlNodePtr cur = NULL;
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (nargs == 0) {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cur = ctxt->context->node;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else if (nargs == 1) {
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xmlXPathObjectPtr obj;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xmlNodeSetPtr nodelist;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int i;
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((ctxt->value == NULL) || (ctxt->value->type != XPATH_NODESET)) {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		"saxon:line-number() : invalid arg expecting a node-set\n");
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    ctxt->error = XPATH_INVALID_TYPE;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    return;
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	obj = valuePop(ctxt);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	nodelist = obj->nodesetval;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if ((nodelist == NULL) || (nodelist->nodeNr <= 0)) {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    xmlXPathFreeObject(obj);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    valuePush(ctxt, xmlXPathNewFloat(-1));
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    return;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	cur = nodelist->nodeTab[0];
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (i = 1;i < nodelist->nodeNr;i++) {
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    int ret = xmlXPathCmpNodes(cur, nodelist->nodeTab[i]);
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    if (ret == -1)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		cur = nodelist->nodeTab[i];
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xmlXPathFreeObject(obj);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		"saxon:line-number() : invalid number of args %d\n",
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		nargs);
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ctxt->error = XPATH_INVALID_ARITY;
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    valuePush(ctxt, xmlXPathNewFloat(xmlGetLineNo(cur)));
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * exsltSaxonRegister:
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Registers the SAXON extension module
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)exsltSaxonRegister (void) {
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xsltRegisterExtModule (SAXON_NAMESPACE,
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			    (xsltExtInitFunction) exsltSaxonInit,
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			    (xsltExtShutdownFunction) exsltSaxonShutdown);
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xsltRegisterExtModuleFunction((const xmlChar *) "expression",
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   SAXON_NAMESPACE,
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   exsltSaxonExpressionFunction);
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xsltRegisterExtModuleFunction((const xmlChar *) "eval",
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   SAXON_NAMESPACE,
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   exsltSaxonEvalFunction);
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)     xsltRegisterExtModuleFunction((const xmlChar *) "evaluate",
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   SAXON_NAMESPACE,
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   exsltSaxonEvaluateFunction);
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    xsltRegisterExtModuleFunction ((const xmlChar *) "line-number",
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   SAXON_NAMESPACE,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				   exsltSaxonLineNumberFunction);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
268