Skip to main content

my own XSLT extension

2 replies [Last post]
raoulb2000
Offline
Joined: 2007-10-22
Points: 0

Hi everyone,
I'm writing my own XSLT extension which in the end should look like this : (that's an example). My problem is that I want the "xpath expression" to be evaluated in the current context. It could contain xsl function calls, reference to previously defined variables, etc .. to make it short, it should be evaluated the same way XSL actually evaluates .

I have no problem using xsl function. In my extension class, when it's time to evaluate de xpath expression I do :

xpathFactory.setXPathFunctionResolver(new org.apache.xalan.extensions.XPathFunctionResolverImpl());

..but It's not so easy for variables, because I don't know how to access to a XPathVariableResolver. Do I have to write one ? ... can I use the one currently used by the XSL processor (the one currently processing my xsl) ? I have access to a VariableStack objects ..I'm not sure about what to do and there may be something I don't understand (obvisouly ;).

So if anyone have an idea or maybe just send me a link to some piece of code I could use to understand better how this works, that would be great because right now (after a lot of search) I'm stuck.

Thanks
Raoul

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
prunge
Offline
Joined: 2004-05-06
Points: 0

Hi Raoul,

According to http://xml.apache.org/xalan-j/extensions.html, your extension element will have a signature along the lines of:

[code]
Type element(org.apache.xalan.extensions.XSLProcessorContext context,
org.apache.xalan.templates.ElemExtensionCall extensionElement)
[/code]

It looks like you might have to build your own XPathVariableResolver - you can use Xalan's ExpressionContext to resolve variables in the context of your extension element. It might look something along the lines of:

[code]
public class XalanVariableResolver implements XPathVariableResolver
{
private org.apache.xalan.extensions.XSLProcessorContext xslContext;

public XalanVariableResolver(org.apache.xalan.extensions.XSLProcessorContext xslContext)
{
this.xslContext= xslContext;
}

public Object resolveVariable(QName variableName)
{
ExpressionContext exprContext = xslContext.getTransformer().getXPathContext().getExpressionContext();
XObject obj = exprContext.getVariableOrParam(variableName);

//Do some conversion from Xalan's XObject to standard object, e.g. String, Integer...
...
}
}
[/code]

Haven't tried it myself - last time I did this was years ago, but hope it helps.

Peter

raoulb2000
Offline
Joined: 2007-10-22
Points: 0

ok, thanks for you're reply prunge, I'll try that.

:)