You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by xavier gibouin <xa...@axonie.com> on 2002/11/05 18:59:26 UTC

Re: incrementing variable

hi this is my xsl code :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd">
<xsl:variable name="x" select="10"/>
 <xsl:template name="codebarre">
  <xsl:variable name="index" select="string-length(codebarre)"/>
  
  <xsl:param name="i" select="0"/>

  <xsl:if test="$i &lt; $index">
   <xsl:choose>
    <xsl:when test="substring(codebarre,$i+1,1)='1'">
     <fo:block><xsl:value-of select="$x"/></fo:block>
     <xsl:variable name="x" select="$x + 4"/>
    </xsl:when>
    <xsl:when test="substring(codebarre,$i+1,1)='2'">
     <fo:block><xsl:value-of select="$x"/></fo:block>
     <xsl:variable name="x" select="$x + 1.5"/>
    </xsl:when>
    <xsl:when test="substring(codebarre,$i+1,1)='3'">
     <fo:block><xsl:value-of select="$x"/></fo:block>
     <xsl:variable name="x" select="$x + 3"/>
    </xsl:when>
    <xsl:when test="substring(codebarre,$i+1,1)='4'">
     <fo:block><xsl:value-of select="$x"/></fo:block>
     <xsl:variable name="x" select="$x + 1.5"/>
    </xsl:when>
   </xsl:choose>

   <xsl:call-template name="codebarre">
    <xsl:with-param name="i" select="$i+1"/>
   </xsl:call-template>

  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

this code always writes
10
10
10
10
10
...

$x is not incremented

any ideas?

thanks a lot

Xavier Gibouin
Axonie
Espace Mercoeur
8, rue Mercoeur
44000 Nantes
02.40.48.53.23
xavier.gibouin@axonie.com
  ----- Original Message ----- 
  From: Paul Washinger 
  To: fop-user@xml.apache.org 
  Sent: Tuesday, November 05, 2002 5:52 PM
  Subject: Re: variable and svg



  put braces around the variable when it is outside an xsl tag:

        <rect height="50" width="4.0" x="{$x}" y="0.0"/>



Re: incrementing variable

Posted by "J.Pietschmann" <j3...@yahoo.de>.
xavier gibouin wrote:
> As my xml file is before traited by a java program in odrer generate 
> other tag. This one will completely generate tag for creating barrecode so

If you want to generate SVG barcodes, why don't you
use the barcode XSL form RenderX?
  http://www.renderx.com/barcodes.html

J.Pietschmann


Re: incrementing variable

Posted by xavier gibouin <xa...@axonie.com>.
As my xml file is before traited by a java program in odrer generate other tag. This one will completely generate tag for creating barrecode so

before : xml file :
<barrcecode>012341223</barrcode>
 but impossible to realise the svg barrce code 

after : xml fil after transformation by java programm
<barrecode>
<number>1123412134121134211231412134242411</number>
   <svg height="50pt" width="400pt"/>
   <rec width="1.5" x="4.0"/>
   <rec width="1.5" x="7.0"/>
   <rec width="1.5" x="10.0"/>
   <rec width="1.5" x="14.5"/>
   <rec width="1.5" x="100"/>
    ...
</barrcode>

xsl file :

<fo:instream-foreign-object>
   <xsl:variable name="w" select="codebarre/svg/@width"/>
   <xsl:variable name="h" select="codebarre/svg/@height"/>

   <svg xmlns="http://www.w3.org/2000/svg" width="{$w}" height="{$h}" x="0">
    <g id="codebarre" fill="#000000">
     <xsl:for-each select="codebarre/rec">
     <xsl:variable name="w" select="@width"/>
     <xsl:variable name="x" select="@x"/>
     <rect x="{$x}" width="{$w}" height="50"/>
     </xsl:for-each>
    </g>
   </svg>
  </fo:instream-foreign-object>


Xavier Gibouin
Axonie
Espace Mercoeur
8, rue Mercoeur
44000 Nantes
02.40.48.53.23
xavier.gibouin@axonie.com
  ----- Original Message ----- 
  From: Oleg Tkachenko 
  To: fop-user@xml.apache.org 
  Sent: Wednesday, November 06, 2002 1:45 PM
  Subject: Re: incrementing variable


  Phil Dickinson wrote:

  > John is right about not being able to change variables and I think that
  > in order to achieve the effect you want, you should consider using some
  > Java code inside your XSL.

  Well, extensions are especially good as last resort, but incrementing 
  variables in a side effect free langauge like xslt sounds very ugly. I 
  wouldn't suggest people to go this way, but instead to stop thinking 
  procedurally and to start using real power of xslt.

  -- 
  Oleg Tkachenko
  eXperanto team
  Multiconn Technologies, Israel



Re: incrementing variable

Posted by Oleg Tkachenko <ol...@multiconn.com>.
Phil Dickinson wrote:

> John is right about not being able to change variables and I think that
> in order to achieve the effect you want, you should consider using some
> Java code inside your XSL.

Well, extensions are especially good as last resort, but incrementing 
variables in a side effect free langauge like xslt sounds very ugly. I 
wouldn't suggest people to go this way, but instead to stop thinking 
procedurally and to start using real power of xslt.

-- 
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel


RE: incrementing variable

Posted by Phil Dickinson <ph...@aicom.co.uk>.
Xavier,

John is right about not being able to change variables and I think that in
order to achieve the effect you want, you should consider using some Java
code inside your XSL.  I had a similar problem when trying to track
indenting levels affected by different template matches.  Of course outside
of each template match that defines the variable, it does not exist so is
actually pretty useless!

One approach

1) Create a Java class like so
package com.myplace.utils.XSLVariable;

public class XSLVariable
{
    public static void setVar( long NewVal )
    {
        ms_lVariable    = NewVal;
    }

    public static long getVar()   { return ms_lVariable; }

    private static long ms_lVariable    = 0;
}

2) Compile the class and JAR up.
3) Add the JAR file to the classpath for your FOP/XSL processor.
4) Create a namespace in the XSL file as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:utils="com.myplace.utils.XSLVariable" version="1.0">

5) Then use a dummy variable to call get and set methods to modify your
variable.

  <xsl:variable name="temp"
select="utils:XSLVariable.setVar(utils:XSLVariable.getVar() + 5)"/>

  or value-of to output

  <xsl:value-of select="utils:XSLVariable.getVar()"/>


Hope this is useful. There is more information on how to do this in the
Apache Xalan help, but I can't remember exactly where just now.

Phil














  -----Original Message-----
  From: John Gentilin [mailto:gentijo@eyecatching.com]
  Sent: 06 November 2002 00:19
  To: fop-user@xml.apache.org
  Subject: Re: incrementing variable


  You need to read the XSL Spec or M Kay's XSLT book about
  side effect free programming. Variables in XSL can only be assigned
  once and never modified.
  Regards
  John G

  xavier gibouin wrote:

    hi this is my xsl code : <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:svg="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd">
    <xsl:variable name="x" select="10"/>
     <xsl:template name="codebarre">
      <xsl:variable name="index" select="string-length(codebarre)"/>
      <xsl:param name="i" select="0"/>   <xsl:if test="$i &lt; $index">
       <xsl:choose>
        <xsl:when test="substring(codebarre,$i+1,1)='1'">
         <fo:block><xsl:value-of select="$x"/></fo:block>
         <xsl:variable name="x" select="$x + 4"/>
        </xsl:when>
        <xsl:when test="substring(codebarre,$i+1,1)='2'">
         <fo:block><xsl:value-of select="$x"/></fo:block>
         <xsl:variable name="x" select="$x + 1.5"/>
        </xsl:when>
        <xsl:when test="substring(codebarre,$i+1,1)='3'">
         <fo:block><xsl:value-of select="$x"/></fo:block>
         <xsl:variable name="x" select="$x + 3"/>
        </xsl:when>
        <xsl:when test="substring(codebarre,$i+1,1)='4'">
         <fo:block><xsl:value-of select="$x"/></fo:block>
         <xsl:variable name="x" select="$x + 1.5"/>
        </xsl:when>
       </xsl:choose>    <xsl:call-template name="codebarre">
        <xsl:with-param name="i" select="$i+1"/>
       </xsl:call-template>   </xsl:if>
     </xsl:template>
    </xsl:stylesheet> this code always writes1010101010... $x is not
incremented any ideas? thanks a lot Xavier Gibouin
    Axonie
    Espace Mercoeur
    8, rue Mercoeur
    44000 Nantes
    02.40.48.53.23
    xavier.gibouin@axonie.com

      ----- Original Message -----
      From: Paul Washinger
      To: fop-user@xml.apache.org
      Sent: Tuesday, November 05, 2002 5:52 PM
      Subject: Re: variable and svg


      put braces around the variable when it is outside an xsl tag:

            <rect height="50" width="4.0" x="{$x}" y="0.0"/>





Re: incrementing variable

Posted by John Gentilin <ge...@eyecatching.com>.
You need to read the XSL Spec or M Kay's XSLT book about
side effect free programming. Variables in XSL can only be assigned
once and never modified.

Regards
John G

xavier gibouin wrote:

> hi this is my xsl code : <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format"
> xmlns:svg="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd">
> <xsl:variable name="x" select="10"/>
>  <xsl:template name="codebarre">
>   <xsl:variable name="index" select="string-length(codebarre)"/>
>
>   <xsl:param name="i" select="0"/>   <xsl:if test="$i &lt; $index">
>    <xsl:choose>
>     <xsl:when test="substring(codebarre,$i+1,1)='1'">
>      <fo:block><xsl:value-of select="$x"/></fo:block>
>      <xsl:variable name="x" select="$x + 4"/>
>     </xsl:when>
>     <xsl:when test="substring(codebarre,$i+1,1)='2'">
>      <fo:block><xsl:value-of select="$x"/></fo:block>
>      <xsl:variable name="x" select="$x + 1.5"/>
>     </xsl:when>
>     <xsl:when test="substring(codebarre,$i+1,1)='3'">
>      <fo:block><xsl:value-of select="$x"/></fo:block>
>      <xsl:variable name="x" select="$x + 3"/>
>     </xsl:when>
>     <xsl:when test="substring(codebarre,$i+1,1)='4'">
>      <fo:block><xsl:value-of select="$x"/></fo:block>
>      <xsl:variable name="x" select="$x + 1.5"/>
>     </xsl:when>
>    </xsl:choose>    <xsl:call-template name="codebarre">
>     <xsl:with-param name="i" select="$i+1"/>
>    </xsl:call-template>   </xsl:if>
>  </xsl:template>
> </xsl:stylesheet> this code always writes1010101010... $x is not
> incremented any ideas? thanks a lot Xavier Gibouin
> Axonie
> Espace Mercoeur
> 8, rue Mercoeur
> 44000 Nantes
> 02.40.48.53.23
> xavier.gibouin@axonie.com
>
>      ----- Original Message -----
>      From: Paul Washinger
>      To: fop-user@xml.apache.org
>      Sent: Tuesday, November 05, 2002 5:52 PM
>      Subject: Re: variable and svg
>
>
>
>      put braces around the variable when it is outside an xsl
>      tag:
>
>            <rect height="50" width="4.0" x="{$x}" y="0.0"/>
>
>



RE: incrementing variable

Posted by Phil Dickinson <ph...@aicom.co.uk>.
Xavier,

Each time you use the syntax <xsl:variable name="x".../> you are declaring a
new local variable, not modifying the existing one!

Since your output text occurs before the new declaration, the global value
is output displayed and this is always ten.

Try moving the fo:block section to after the declaration of the new
variable.

Phil Dickinson
Aicom Limited

  -----Original Message-----
  From: xavier gibouin [mailto:xav@axonie.com]
  Sent: 05 November 2002 17:59
  To: fop-user@xml.apache.org
  Subject: Re: incrementing variable


  hi this is my xsl code :

  <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:svg="http://www.w3.org/Graphics/SVG/SVG-19990812.dtd">
  <xsl:variable name="x" select="10"/>
   <xsl:template name="codebarre">
    <xsl:variable name="index" select="string-length(codebarre)"/>

    <xsl:param name="i" select="0"/>

    <xsl:if test="$i &lt; $index">
     <xsl:choose>
      <xsl:when test="substring(codebarre,$i+1,1)='1'">
       <fo:block><xsl:value-of select="$x"/></fo:block>
       <xsl:variable name="x" select="$x + 4"/>
      </xsl:when>
      <xsl:when test="substring(codebarre,$i+1,1)='2'">
       <fo:block><xsl:value-of select="$x"/></fo:block>
       <xsl:variable name="x" select="$x + 1.5"/>
      </xsl:when>
      <xsl:when test="substring(codebarre,$i+1,1)='3'">
       <fo:block><xsl:value-of select="$x"/></fo:block>
       <xsl:variable name="x" select="$x + 3"/>
      </xsl:when>
      <xsl:when test="substring(codebarre,$i+1,1)='4'">
       <fo:block><xsl:value-of select="$x"/></fo:block>
       <xsl:variable name="x" select="$x + 1.5"/>
      </xsl:when>
     </xsl:choose>

     <xsl:call-template name="codebarre">
      <xsl:with-param name="i" select="$i+1"/>
     </xsl:call-template>

    </xsl:if>
   </xsl:template>
  </xsl:stylesheet>

  this code always writes
  10
  10
  10
  10
  10
  ...

  $x is not incremented

  any ideas?

  thanks a lot

  Xavier Gibouin
  Axonie
  Espace Mercoeur
  8, rue Mercoeur
  44000 Nantes
  02.40.48.53.23
  xavier.gibouin@axonie.com
    ----- Original Message -----
    From: Paul Washinger
    To: fop-user@xml.apache.org
    Sent: Tuesday, November 05, 2002 5:52 PM
    Subject: Re: variable and svg



    put braces around the variable when it is outside an xsl tag:

          <rect height="50" width="4.0" x="{$x}" y="0.0"/>