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 June 29th, 2012, 04:16 AM
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 12
Thanked 0 Times in 0 Posts
Default Obtaining the max value out of a series.

Hello,

I am trying to create a graph of my data using <svg> but in order to scale the y axis correctly I need to find the max of a series of numbers.

To do this I thought the best approach would be to use something like this:

Code:
<!-- Plot Bars -->
<xsl:variable name="maxY" as="xs:decimal" select="0"/>
<xsl:for ...>
    ...
    <xsl:if test="@attr_1 >= $maxY">
        <xsl:value-of select="$maxY = @attr_1"/>
    </xsl:if>
    ...
</xsl:for>
<!-- Use maxY here to plot Y axis -->
But this does nothing, I'm left with 0, so I then thought:

Code:
<!-- Plot Bars -->
<xsl:variable name="maxY" as="xs:decimal" select="0"/>
<xsl:for ...>
    ...
    <xsl:value-of select="$maxY = max($maxY, @attr_1)"/>
    ...
</xsl:for>
<!-- Use maxY here to plot Y axis -->
But this produces and error during compilation of : Required item type of second argument of max() is xs:string; supplied value has item type xs:decimal

My XSL is Schema aware so it knows that @attr_1 really is a xs:decimal so needless to say I am quite confused with this as the W3C docs imply that it will return the max of a series of comma limited entries!

So I would appreciate your thoughts on this please.

--
Bill
 
Old June 29th, 2012, 04:33 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't update a variable in XSLT - its a functional programming language.

The following should give you the max of all attr_1 attribute values of the child element.

<xsl:variable name="maxY" select="max(element/@attr_1)"/>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
BillMoo (June 29th, 2012)
 
Old June 29th, 2012, 04:45 AM
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
You can't update a variable in XSLT - its a functional programming language.

The following should give you the max of all attr_1 attribute values of the child element.

<xsl:variable name="maxY" select="max(element/@attr_1)"/>
And indeed it does. Thank you very much, I'm not making excuses for myself but as a C++ programmer my approach was logical and sane.

--
Bill
 
Old June 29th, 2012, 05:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The actual error message is because the first argument of max() must be a sequence, and the second (optional) argument is a collation to be used for the comparison. So to get the max of X and Y, you need max((X,Y)) rather than max(X,Y) - if you use the latter form, Y is taken as the collation.

But as Sam has explained, the underlying problem in your code is deeper: you want to compute the max by supplying the whole sequence to the function, not by computing the max of each pair of values in turn.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
obtaining handle of the window C# nirmal_thapa C# 0 January 30th, 2007 09:10 AM
Obtaining User Logon dearnne Access 1 September 2nd, 2004 01:39 PM
Programmatically obtaining info on macros tcarnahan Access VBA 0 August 14th, 2004 11:42 PM
Obtaining information from a form Ashleek007 PHP How-To 1 March 1st, 2004 09:51 PM
Obtaining a variable from the first letter... starsol Beginning PHP 3 August 18th, 2003 01:49 PM





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