You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Loïc Trégan <lt...@hotmail.com> on 2000/12/20 17:32:12 UTC

questions on extensions (xalan 1.2.2)

1) Is it possible for a element extension to return a NodeList ? the
following code does not work (the inside of my extension element is never
executed, altough the java method itself is called) ;

JAVA code :
  public NodeList foreachlink( XSLProcessorContext context,
                            ElemExtensionCall extElem) throws Exception {

    VectorNode result = new VectorNode();
    result.add( context.sourceNode );
    return result;
  }

XSL code :
    <myext:foreachlink >
          not passing here :-(
    </myext:foreachlink >

I works with a extension function used in a xsl:for-each.

2) I convert the element to a function :
    <xsl:for-each select="myext:getLinksTo('ACT1')"> needs NodeList
getLinksTo( String)

   <xsl:variable name='activityID'>ACT1</xsl:variable>
    <xsl:for-each select="myext:getLinksTo($activityID)"> needs NodeList
getLinksTo( ResultTreeFrag)

wish :  could it possible for xalan to map getLinksTo($activityID) to
getLinksTo(String) since $activityID has a single text node ?

3) my function need some XPath evaluation; basically the code is (to be
included in xalan 2 extensions documentation ??):

  private XObject ApplyXPath( XSLProcessorContext context,
                              Node sourceNode,
                              String select,
                              boolean stopAtFirst ) throws Exception {
    XPathSupport xpathSupport = context.processor.getExecContext();;
    XPathProcessor processor = new
org.apache.xalan.xpath.XPathProcessorImpl(xpathSupport);
    XPath  xpath = new XPath();
    processor.initXPath(xpath, select, extElem);
    return xpath.execute( xpathSupport, sourceNode, context.styleSheetTree,
null, null, stopAtFirst);
  }

a) is it ok to use the stylesheet as prefix resolver ? there is not prefix
added "after" the stylesheet that it is unknown to it ?

b) I can retrieve context as arguments of an extension element; but how can
I retrieve it for an extension function ? I use a myext:init element to save
it as global variable so that it can retrieved by the extension function,
but there might be something else. If no, as a wish I would like that xalan
try to match "getLinksTo( XSLProcessorContext context, <any> );" so that my
element function can retrieve a processor if it needs so (I mean, I see no
reason that extension element have a ProcessContext and function extension
not).

thanks for attention.
loic

Re: questions on extensions (xalan 1.2.2)

Posted by Gary L Peskin <ga...@firstech.com>.
Loïc Trégan wrote:
> 
> 1) Is it possible for a element extension to return a NodeList ? the
> following code does not work (the inside of my extension element is never
> executed, altough the java method itself is called) ;
> 
> JAVA code :
>   public NodeList foreachlink( XSLProcessorContext context,
>                             ElemExtensionCall extElem) throws Exception {
> 
>     VectorNode result = new VectorNode();
>     result.add( context.sourceNode );
>     return result;
>   }
> 
> XSL code :
>     <myext:foreachlink >
>           not passing here :-(
>     </myext:foreachlink >
> 
> I works with a extension function used in a xsl:for-each.

You need to process the element body yourself.  See the redirect
extension code for an example where it calls
context.processor.writeChildren.  I'm not sure exactly what you want to
do but depending on your situation, there should be a method in
XSLTEngineImpl (accessed via context.processor) that should prove
helpful.  Come back here with an example if you need help to figure it
out.

> 2) I convert the element to a function :
>     <xsl:for-each select="myext:getLinksTo('ACT1')"> needs NodeList
> getLinksTo( String)
> 
>    <xsl:variable name='activityID'>ACT1</xsl:variable>
>     <xsl:for-each select="myext:getLinksTo($activityID)"> needs NodeList
> getLinksTo( ResultTreeFrag)
> 
> wish :  could it possible for xalan to map getLinksTo($activityID) to
> getLinksTo(String) since $activityID has a single text node ?

Not with XalanJ1 as it sits now.  You could change your call to

  <xsl:for-each select="myext:getLinksTo(string($activityID))">

or you could have a separate method that takes a ResultTreeFrag
parameter and calls

  new XRTreeFrag(yourRTF).str();

> 3) my function need some XPath evaluation; basically the code is (to be
> included in xalan 2 extensions documentation ??):
> 
>   private XObject ApplyXPath( XSLProcessorContext context,
>                               Node sourceNode,
>                               String select,
>                               boolean stopAtFirst ) throws Exception {
>     XPathSupport xpathSupport = context.processor.getExecContext();;
>     XPathProcessor processor = new
> org.apache.xalan.xpath.XPathProcessorImpl(xpathSupport);
>     XPath  xpath = new XPath();
>     processor.initXPath(xpath, select, extElem);
>     return xpath.execute( xpathSupport, sourceNode, context.styleSheetTree,
> null, null, stopAtFirst);
>   }
> 
> a) is it ok to use the stylesheet as prefix resolver ? there is not prefix
> added "after" the stylesheet that it is unknown to it ?

Should be.

> b) I can retrieve context as arguments of an extension element; but how can
> I retrieve it for an extension function ? I use a myext:init element to save
> it as global variable so that it can retrieved by the extension function,
> but there might be something else. If no, as a wish I would like that xalan
> try to match "getLinksTo( XSLProcessorContext context, <any> );" so that my
> element function can retrieve a processor if it needs so (I mean, I see no
> reason that extension element have a ProcessContext and function extension
> not).

Neither do I.  This facility has been added in XalanJ2 and it should be
explained in the extension documentation in the upcoming beta.  There is
a new interface called org.apache.xalan.extensions.ExpressionContext. 
If it is the type of the first argument in the java argument list, an
object implementing this interface will be passed to the extension
function and will provide methods for accessing various parts of the
environment in which the function has been invoked.

I don't see the corresponding improvements that you suggest being made
in XalanJ1 at this time because the extension code there is quite
convoluted.  If you (or someone else) would like to make the changes
yourself and contribute them, I'd be happy to review and commit them. 
However, the XalanJ1 extension mechanism is quite fragile and if I were
you, I'd wait for XalanJ2 unless your need is super-urgent.  I believe
the new beta is due out tomorrow or thereabouts and it should be of beta
quality (as opposed to alpha) and ready to be banged upon.

Gary