Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2003, 12:50 AM
Registered User
 
Join Date: Oct 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jrwarwick
Default Finding the maximum and using it outside the IF

Hello!
Here is my headache:
I am transforming xml into svg to create a bar graph.
In order to set up the graph axes I need to figure the maximum value to be displayed so that it will be accommodated by the graph space.

Cut down input xml:
<report>
 <item value="7"/>
 <item value="4"/>
 <item value="2"/>
 <item value="2"/>
 <item value="1"/>
</report>

XSL:
<xsl:for-each select="item">
 <xsl:sort data-type="number" order="ascending" select="@value"/>
 <xsl:if test="position()=1">
  <xsl:variable name="maxy" select="ceiling(@value div 10) * 10"/>
  <xsl:variable name="stepy" select="($ymax - $ymin) div $maxy"/>
 </xsl:if>
</xsl:for-each
>

Now the stepy variable is only available inside the 'if 'block and therefore I can only use it to measure the relative length of the largest item bar to draw; all the other bars cant get this unit length value to measure their own bars.
How can I retrieve the maximum value so that it is usable in the rest of the template??
I believe this sort of info gathering is necessary in many many applications surely it is possible!
 
Old October 17th, 2003, 03:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

You just need to write the max to a variable with stylesheet scope. The way to do that is set-up a template which calc's the max and call that template from within an <xsl:variable> definition. Here's an example:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ...

  <xsl:variable name="maxNum">
    <xsl:call-template name="getMax">
      <xsl:with-param name="nodes" select="/report/item"/>
    </xsl:call-template>
  </xsl:variable>
  ...

  <xsl:template name="getMax">
    <xsl:param name="nodes"/>
    <xsl:choose>
      <xsl:when test="not($nodes)">NaN</xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="$nodes">
          <xsl:sort data-type="number" order="descending"/>
          <xsl:if test="position() = 1">
            <xsl:value-of select="number(.)"/>
          </xsl:if>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  ...
hth
Phil
 
Old October 17th, 2003, 03:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I haven't tried this for your example bit normally you put the variable declaration around the test:
Code:
<xsl:variable name="maxValue">
  <xsl:for-each select="item">
    <xsl:sort data-type="number" order="ascending" select="@value"/>
    <xsl:if test="position()=1"> 
      <xsl:value-of select="@value"/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>
Then use $maxValue to produce your other variables.

--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get maximum value from database Manisha0605 .NET Framework 2.0 6 June 17th, 2007 12:50 PM
Maximum Users Brendan Bartley Access 1 March 15th, 2007 02:34 PM
parameter: 6 selections maximum krema Reporting Services 0 February 20th, 2006 05:07 AM
Row Maximum bmains SQL Server 2000 2 October 7th, 2004 08:39 PM
Select Maximum of Sum acko SQL Server 2000 3 June 17th, 2004 11:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.