Re: Selecting a certain amount of characters with XSL

Subject: Re: Selecting a certain amount of characters with XSL
From: Tom Myers <tom.myers@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 09 Mar 2000 13:26:47 -0500
David Carlyle was asking

>do you want an XML tree with a limit of 1000 on the number of
>characters appearing in character data
>
>or
>
>do you want an XML tree which when linearised to a file results in
>at most 1000 characters including markup appearing in that file.
>
>In the latter case what do you want to do if you reach the 1000 limit,
>just truncate the file and leave the result not well formed, or
>do you want to still close every open element perhaps deleting some
>more characters to stay within the limit.

and my own problem, which may not be too similar to that which
started this thread, is simply that there's an overall limit of
not too much over 1K on the total size of a WML deck sent to a
cellular phone...but a not-well-formed result is not acceptable.

So the best I've been able to think of is to say that the stylesheet
overall is going to be fixed, mostly generated manually, but that
the character data within it will be pruned for a total of no more
than X. I'm doing that with yet another xt java extension function 
(I've been doing a lot of these lately) called this way:

---------
    xmlns:pruner="http://www.jclark.com/xt/java/MyNa.jspUtil.XSLPrune";
....(skip to usage of class)

  the zip code was 
<xsl:value-of select="pruner:prune('xxx',string($Zip),20)"/>
  and the temp was 
<xsl:value-of select="pruner:prune('xxx',string($Temp),20)"/>
at 
<xsl:value-of select="pruner:prune('xxx',string($RecTime),20)"/>
-----------

Here the 'xxx' is a label for the particular pruner, which for me
will usually be a SessionID; the $Zip, $Temp and so forth are the
values I want to produce here, and "20" is the limit, set low for
testing---the limit is somewhat deceptive in that it will
only be looked at the first time that the 'xxx' pruner is invoked,
and of course I don't know or want to know which time that will
be. The XSLPrune code is simple enough:
-----------
package MyNa.jspUtil; // (c) Tom Myers, 2000

public class XSLPrune  { // a utility for pruning XSL output.

private static java.util.Hashtable pruners=null;
private int outLimit;

public int getOutLimit(){return outLimit;}
public void setOutLimit(int outLimit){this.outLimit=outLimit;}

private XSLPrune(int outLimit){setOutLimit(outLimit);}

public synchronized static String prune(String label,String out,Double
maxOut){
  if(null==pruners)pruners=new java.util.Hashtable();
  int outLim=maxOut.intValue();
  XSLPrune pruner=(XSLPrune)(pruners.get(label));
  if(null==pruner)pruners.put(label,pruner=new XSLPrune(outLim));
  outLim=pruner.getOutLimit();
  if(outLim<0)return out;
  int len=out.length();
  if(outLim>len){pruner.setOutLimit(outLim-len);return out;}
  out=out.substring(0,outLim);
  pruner.setOutLimit(0);
  return out;
}
}
-----------

and it seems to work in xt, but I'm not sure I like it...but I
don't see a better way yet.

Tom Myers


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


Current Thread