Thanks for the suggestion, David (and thanks to everyone else for other
suggestions and info on general Xalan architecture).

Unfortunately, this didn't resolve it. So I did what all good
programmers do when the code they've copied from one source doesn't
work: steal from a different source instead :-)

My original code was based on stuff in the xalan.extensions package;
going back to the source for the value-of operator itself solved the
problem. Below is some working code for evaluating select expressions
within extension elements, in case this is of use to someone else. I
don't entirely understand why it works, but it does. 

BTW, this probably implies that the code in the extensions package which
evaluates a select expression is wrong (at least, doesn't handle local
variables correctly).

Cheers,

Simon

    private static String doSelect(
    XSLProcessorContext procContext, 
    ElemExtensionCall extensionElement,
    String selectStr)
    throws TransformerException
    {
        String nodeText = "";
        if ((selectStr != null && selectStr.length() != 0))
        {
            TransformerImpl transformer = procContext.getTransformer();
            XPathContext xpContext = transformer.getXPathContext();
               
            try
            {
                xpContext.pushNamespaceContext(extensionElement);
                int current = xpContext.getCurrentNode();
                xpContext.pushCurrentNodeAndExpression(current,current);
                
                XPath dynamicXPath = new XPath(
                    selectStr,
                    xpContext.getSAXLocator(),
                    xpContext.getNamespaceContext(),
                    XPath.SELECT,
                    transformer.getErrorListener());

                Expression expression = dynamicXPath.getExpression();
                
                XObject xobj1 = expression.execute(xpContext);
                xpContext.popCurrentNodeAndExpression();
                xpContext.popNamespaceContext();
                nodeText = xobj1.toString();
            }
            catch(TransformerException te)
            {
                String msg =
                    "failed to evaluate expression at line [" 
                    + xpContext.getSAXLocator().getLineNumber()
                    + "] : " + te.getMessage();
                
                reportError(procContext, extensionElement, msg);
                return "";
            }
        }

        return nodeText;
    }


[NB: the reportError method is a custom method, not a xalan method]

-----Original Message-----
From: David Marston/Cambridge/IBM [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, June 12, 2002 2:11 AM
To: [EMAIL PROTECTED]
Subject: Re: Evaluating xpath expression in extension element



Simon Kitching writes:
>I am trying to implement an extension element like this:
>  <ext:field select="..."  myattr1="..." myattr2="..."/>
>where the select should be able to contain anything that would normally

>be valid in an <xsl:value-of select="..."/> expression. I have got this

>mostly working, but my code fails for...
>  <ext:field select="$foo"/>
>My code which evaluates the specified select statement reports that the

>variable "foo" cannot be found... Variable "foo" is definitely in 
>scope; replacing a call to my extension with code like <xsl:value-of 
>select="$foo"/> works fine.

This may involve lazy evaluation of variables. What happens if you do
both <xsl:value-of select="$foo"/> <ext:field select="$foo"/> in that
order? The value-of forces Xalan to evaluate $foo without involving your
extension. Even if it doesn't solve the problem, it would be a good
clue. .................David Marston



Reply via email to