xsl-list
[Top] [All Lists]

Re: Global extension function

2003-02-24 09:47:33
Hmmmm.... this post has struck an interesting / hopeful thought in my head.
I see this post has an example of a javascript function included in the
XSLT.  I'm wondering if functions can be written and used during the
transformation.  I was assuming NOT.  I've seen function written out before
is XSLT, however was assuming these were used later, on the client side.

So, can you write custom function for use by XSLT processing?  If so, which
languages are supported?

Karl


----- Original Message -----
From: "TP" <tpass001(_at_)hotmail(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Monday, February 24, 2003 9:24 AM
Subject: Re: [xsl] Global extension function


Using extension functions to get round the fact that XSLT doesn't allow
you to update variables is a really bad idea, save in exceptional
circumstances (for example, it would be OK if the counter is for
instrumenting your stylesheet, and the value of the counter doesn't
affect the result). You are abusing the language paradigm, and this
always leads to tears in the end. In your case, it has led to tears
already, because as you have discovered, extension functions are not
portable.

Find out why the stylesheet is using a counter, and replace it with
native XSLT constructs if you possibly can. The chances are that you can
achieve the required effect simply by using position() or xsl:number.

Michael Kay

I know what you are saying Michael and I agree with you. I removed the
extensions from 2 of my stylesheets.

What I want to do is change the background color of the table every 3
lines.
This is the only way that I have found to do this. If you or someone have
some ideas on how to solve this using standard xsl, I would appreciate it.

The code snippet is
==========================
<div>
           <xsl:attribute name="style">background-color:<xsl:value-of
select="counter:color()"/>;color:black;margin-top: 0px;margin-left:
2px;margin-right: 2px;margin-bottom:-19px;</xsl:attribute>
==========================
<lxslt:component prefix="counter"
                   elements="init incr" functions="read">
    <lxslt:script lang="javascript">

 var count=0;
 var hit=0;

 function color(){
  if(hit != 3){
      hit=hit+1
      return "white";
     }
     else {
     if(count != 2){
      count=count+1;
      return "#d4e3ff";
     }
     count=0;
     hit=0;
     return "#d4e3ff";
    }
   }
 </lxslt:script>
</lxslt:component>
==========================
Thanks.

TP.
----- Original Message -----
From: "Michael Kay" <michael(_dot_)h(_dot_)kay(_at_)ntlworld(_dot_)com>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Saturday, February 22, 2003 9:12 AM
Subject: RE: [xsl] Global extension function


Using extension functions to get round the fact that XSLT doesn't allow
you to update variables is a really bad idea, save in exceptional
circumstances (for example, it would be OK if the counter is for
instrumenting your stylesheet, and the value of the counter doesn't
affect the result). You are abusing the language paradigm, and this
always leads to tears in the end. In your case, it has led to tears
already, because as you have discovered, extension functions are not
portable.

Find out why the stylesheet is using a counter, and replace it with
native XSLT constructs if you possibly can. The chances are that you can
achieve the required effect simply by using position() or xsl:number.

Michael Kay
Software AG
home: Michael(_dot_)H(_dot_)Kay(_at_)ntlworld(_dot_)com
work: Michael(_dot_)Kay(_at_)softwareag(_dot_)com

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf 
Of TP
Sent: 21 February 2003 16:40
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Global extension function


I currently use xalan for my transformations.

I use 2 simple javascript functions that I have to because I
inherited them from <xsl:eval> days. I am planning on trying
other xslt processors but I cannot because I have been tied
down to using xalan. Looked up and found exslt, maybe this
can be used for my simple javascript functions.

The code is
=======================
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:lxslt="http://xml.apache.org/xslt";
xmlns:counter="MyCounter" extension-element-prefixes="counter"
    version="1.0">
<lxslt:component elements="init incr" functions="read"
prefix="counter">
  <lxslt:script lang="javascript">
      var out=0;
   var idi;

   function outID(){
  var id="Out"+out;
  idi=id;
  out=out+1;
  return id;
    }

   function inID(){
  return idi+"d";
   }

   </lxslt:script>
 </lxslt:component>
===========================================

Is there a quick solution to this such that I can include a
global kind of extension function and change processors as i
choose with the TRAX functionality.

Thanks

TP.

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



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


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




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