Re: [xsl] java extension element and xsl variable problem

Subject: Re: [xsl] java extension element and xsl variable problem
From: Keith Lynch <klynch@xxxxxxxxxxxx>
Date: Tue, 22 Feb 2005 10:01:35 +0100
I asked the following question around this time last week and after searching the net, dredging newsgroups and posting I finally got my answer. There seemed like a few people who had the same question. Wierdly the only place I found my answer was not even in the xalan samples but by getting the actual xalan source and seeing how they do it. They really should have an example showing the rest of the world. + commenting all methods in their API doc with 'for advance use only' is quite annoying. I want to use the bloody thing I don't want to be told I'm too childish to be allowed near the API. If their is anyone out there on the Xalan doc team. Shame on you. 'Not yet documented' would be a more realistic and less intimidating solution.

Anyway my guess was right one must dynamically evaluate the XPath expression.

This is how they do it for a filename... not tricky but without examples or sufficent API doc it shows how 10 lines of code can consume alomost 20 hours of work.

private String getFilename(XSLProcessorContext context, ElemExtensionCall elem)
throws java.net.MalformedURLException,
java.io.FileNotFoundException,
java.io.IOException,
javax.xml.transform.TransformerException
{
String fileName;
String fileNameExpr
= ((ElemExtensionCall)elem).getAttribute ("select",
context.getContextNode(),
context.getTransformer());
if(null != fileNameExpr)
{
org.apache.xpath.XPathContext xctxt
= context.getTransformer().getXPathContext();
XPath myxpath = new XPath(fileNameExpr, elem, xctxt.getNamespaceContext(), XPath.SELECT);
XObject xobj = myxpath.execute(xctxt, context.getContextNode(), elem);
fileName = xobj.str();
if((null == fileName) || (fileName.length() == 0))
{
fileName = elem.getAttribute ("file",
context.getContextNode(),
context.getTransformer());
}
}
else
{
fileName = elem.getAttribute ("file", context.getContextNode(),
context.getTransformer());
}
if(null == fileName)
{
context.getTransformer().getMsgMgr().error(elem, elem,
context.getContextNode(),
XSLTErrorResources.ER_REDIRECT_COULDNT_GET_FILENAME);
//"Redirect extension: Could not get filename - file or select attribute must return vald string.");
}
return fileName;
}


Hi list

I've been fooling around with XSLT for the last two weeks and I hit a brick wall yesterday.

I'm trying to create a java extension element using xalan which takes an xsl variable as a parameter.

<configurator:setProperty key="lang" value="$Language"/>

However I just keep on receiving the literal string "$Language" as my value attribute

public Object setProperty(
     XSLProcessorContext context,
     ElemExtensionCall   extElem)
 {
   try
   {      System.out.println(extElem.getAttribute("key"));
     System.out.println(extElem.getAttribute("value"));
   }
   return null;
}

Formerly I was using an extension function with an XPath string from a select="$variable" and this worked fine.

<xsl:variable name="chapter_number"
select="position() + configurator:getProperty($Whatever)"/>


This worked just fine and my function revieved the value of $Whatever and not the literal string "$Whatever"

However the function doesn't return anything and being obliged to insert an xsl:variable or xsl:value-of or any other senseless xsl:blahblah is
just annoying and basically creates redundant variables. i.e .. NOT CLEAN


My problem hinges around evaluating a variable. In an old snippet I found one could simply call
getVariableOrParam on the root StyleSheet element however the xalan processor uses newer standards with XPath etc. I'm not too sure here. I don't have a lot of xslt exp.


It doesn't seem to be a trivial task. I've also tried sending subelements

<extension:element blahblahblha>
<value>
  <xsl:value-of select"$var"/>
<value>
/<extension:element>

and parsing all childNodes. However when I call getNodeValue I just get an Exception.


Any help or pointers would be greatly appreciated TIA Keith Lynch

p.s I'm using the ant 1.6.2 stylesheet task... under windows XP sp3



--
______________________________________________________________
/ \
& Keith Lynch Mail $ klynch [BT] genigraph.fr &
& &
& GENIGRAPH URL $ http://www.genigraph.fr &
& 104, rue Castagnary Tel $ +33 1 45 33 64 63 &
& F-75015 PARIS - FRANCE Fax $ +33 1 45 33 89 63 &
\ /
//////////////////////////////////////////////////////////////


Current Thread