Hi Paul,

Here's code I use to achieve this effect. The method is called doSelect
(and the param string "selectStr") because it should behave in the same
manner as the select attribute in an <xsl:value-of select="..."/> tag,
ie be evaluated for its string value.


    /**
     * evaluate a string containing an arbitrary expression which may
     * contain variable references, xpath expressions, calls to
functions,
     * etc.
     *
     * @return a string which represents the result of the expression.
     */
    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;
    }

I then call this from my extension methods, like this:

    /**
     * Evaluate the specified expression, and if it is false then report
     * an error and throw an exception which will terminate stylesheet 
     * processing.
     */
    
    public static void assertExpr(
    XSLProcessorContext procContext,
    ElemExtensionCall extensionElement)
    throws TransformerException
    {
        // note that while the Node.getAttribute method never returns
null,
        // the ElemExtensionCall.getAttribute method does!
        String selectStr = extensionElement.getAttribute("select");
        String message = extensionElement.getAttribute("message");
        
        // execute the xpath command in select (if any)
        String nodeText = doSelect(procContext, extensionElement,
selectStr);

        if (!nodeText.equals("true"))
        {
            reportError(procContext, extensionElement, message);
        }
    }
    

I hope this is what you were looking for...

Regards,

Simon

On Fri, 2002-11-29 at 04:48, Paul Brady wrote:
> I get the same result when using v2.4.1. 
> The following piece of code works when evaluating local variables, but not
> for parameters. Is there a similar way to evaluate paramteters?
> 
> XPathContext xctxt = context.getTransformer().getXPathContext();
> XPath myxpath = new XPath("$VarName", elem, xctxt.getNamespaceContext(), 0);
> XObject xobj = myxpath.execute(xctxt, context.getContextNode(), elem);
> String strVarVal = xobj.str();
> 
> Thanks,
> 
> Paul
> 
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
> Sent: 27 November 2002 17:59
> To: Paul Brady
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: Passing params to an extension element
> 
> We fixed a lot of extension problems in Xalan 2.4.1. Please try with this
> latest release to see if it fixes your problem.
> 
> Morris Kwan
> XSLT Development
> IBM Toronto Lab
> Tel: (905)413-3729
> Email: [EMAIL PROTECTED]
> 
> 
> 
>  
> 
>                       Paul Brady
> 
>                       <[EMAIL PROTECTED]        To:
> "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>           
>                       la.com>                  cc:
> 
>                                                Subject:  Passing params to
> an extension element                                    
>                       11/27/2002 12:30
> 
>                       PM
> 
>  
> 
> 
> 
> 
> 
> 
> I'm trying to pass an <xsl:param> into an extension element using Xalan
> 2.3.1.
> 
> If I do something like:
>     <xsl:template name="DocletContentBody">
>         <xsl:param name="docletTemplate"/>
> 
>         <dynamicTemplate:dynamicCallTemplate name="$docletTemplate">
>             <xsl:call-template name="doesntMatter">
>             </xsl:call-template>
>         </dynamicTemplate:dynamicCallTemplate>
>     </xsl:template>
> 
> where dynamicTemplate is my extension element, the value of the name
> attribute evaluates to "$docletTemplate" rather than the value of the
> parameter.
> 
> Any ideas?
> 
> Paul.
> 
> 
> 
-- 
Simon Kitching <[EMAIL PROTECTED]>

Reply via email to