You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by f0 fo <fo...@yahoo.com> on 2001/05/01 23:00:15 UTC

Help: xsl:variable how to declare a global variable

hi !
  I have problem with declaring global varibles,this
is the code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:variable name="outter">15 </xsl:variable>	
<xsl:template match="/">		

<xsl:for-each select="test/value">
<xsl:variable name="outter" select ="number($outter) +
number(10)"  />	
</xsl:for-each>	
	
value = <xsl:value-of select="$outter"/>
</xsl:template>
</xsl:stylesheet>

*************
output is always value = 15

The xml file is 

<?xml version="1.0" encoding="UTF-8"?>
<test>
	<value> 1</value>
	<value> 1</value>
	<value> 1</value>
	<value> 1</value>
	<value> 1</value>
</test>

****************

Can anyone tell me whats the problem is , I went
throu' many sites I was not able to get the idea.
Help is appreciated.

TIA
Kumar




__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org


Re: Help: xsl:variable how to declare a global variable

Posted by "Peter B. West" <pb...@powerup.com.au>.
Kumar,

The short answer is that there is no such thing as a global variable in
xslt.  If you need to count, you have to do it recursively, passing a
parameter into a template.  That parameter can have the same name
(obviously - it's a recursive invocation of the template), but the
parameter value is local to the current invocation of the template, so
it can be incremented.  Attempting to modify the value of a "global"
variable will have no effect after it has been set.

An alternative for your example is to use the builtin function `count'.
E.g. (probably too verbose)

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet
		version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:fo="http://www.w3.org/1999/XSL/Format"
		>
  <xsl:template match="test">
    <xsl:variable name="inner">
      <xsl:value-of select="count(./value)"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$inner = 0">
	<xsl:text>15</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$inner"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>


You could set the default value as a parameter to the template, which
would allow you some flexibility with the default.

Peter


f0 fo wrote:
> 
> hi !
>   I have problem with declaring global varibles,this
> is the code
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format">
> 
> <xsl:variable name="outter">15 </xsl:variable>
> <xsl:template match="/">
> 
> <xsl:for-each select="test/value">
> <xsl:variable name="outter" select ="number($outter) +
> number(10)"  />
> </xsl:for-each>
> 
> value = <xsl:value-of select="$outter"/>
> </xsl:template>
> </xsl:stylesheet>
> 
> *************
> output is always value = 15
> 
> The xml file is
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <test>
>         <value> 1</value>
>         <value> 1</value>
>         <value> 1</value>
>         <value> 1</value>
>         <value> 1</value>
> </test>

-- 
Peter B. West  pbwest@powerup.com.au  http://powerup.com.au/~pbwest
"Lord, to whom shall we go?"

---------------------------------------------------------------------
To unsubscribe, e-mail: fop-dev-unsubscribe@xml.apache.org
For additional commands, email: fop-dev-help@xml.apache.org