This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

How to output an element from within an extension element?


What's the correct way to output an element to the result tree from within
the code of an extension element (or function)?

As a dummy example, let's say I want to write a Javascript extension element
(using the Xalan-Java processor) to output a <p> tag, then process
the child elements of the context node, then output a </p> tag.  The code
I've got at the moment is

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:lxslt="http://xml.apache.org/xslt";
                xmlns:my="MyNamespace"
                extension-element-prefixes="my"
                version="1.0">
                
  <lxslt:component prefix="my" elements="para">
  <lxslt:script lang="javascript">
    <![CDATA[

      function para(xslContext,thisElement) {
        xslContext.outputToResultTree(xslContext.getStylesheet(),"<p>");
        
        xslTrans = xslContext.getTransformer();
        xslTrans.executeChildTemplates(thisElement,
           xslContext.getContextNode(),
           xslContext.getMode(),
           true
        );
        
        xslContext.outputToResultTree(xslContext.getStylesheet(),"</p>");
        
        return null;
      }
    ]]>
  </lxslt:script>
  </lxslt:component>

  <xsl:template match="/">
    <my:para>
      This is a paragraph.
    </my:para>
  </xsl:template>
</xsl:stylesheet>

Of course, this isn't right.  I don't want to output the text strings '<p>'
and '</p>'; I want to output an element node <p>, but I haven't been able to
find any documentation or examples which mention this kind of thing.  (In
fact, I only found out about the .executeChildTemplates method by looking in
the source code for <xalan:write>!  Where *should* I look for a guide to
neat things I can do in Javascript extension elements/functions?)

Second question: Let's say I put the xsl:component and xsl:script stuff
shown above in a separate stylesheet, and then include or import this into
my main stylesheet.  Will the main stylesheet *and any other stylesheets
included/imported by it* be able to 'see' the definition of <my:para>?

All help appreciated,

Mike.

---
Signal Computing Ltd.       20 Nugent Road, The Surrey Research Park,
http://www.signal.co.uk     Guildford, Surrey GU2 7DF, UK
Tel: +44(0)1483 579900    Fax: +44(0)1483 562836
Registered in London, No. 1672944

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]