Project

Profile

Help

max() XPath Function in Saxon 8.2

Added by Anonymous over 19 years ago

Legacy ID: #3014229 Legacy Poster: Curtis (cdfisher07)

The max function used as: <xsl:variable name="rpm" select="max(tokenize($data/DataFile/RunData/RPM,'\r?\n' ))"/> with the node set: <RPM> 30.06 40.05 50.07 60.07 70.08 80.12 89.1 99.06 </RPM> returns: 99.06 ...as long as the number is below 100. If I set 99.06 to 100.xx, 100, or 300, it returns the previous largest number of 89.1. I have tested this with max numbers in every position and as long as I stay below 100, it works fine. I am ttempting to set a range for an SVG graph and finding the max RPM is critical to creating the proper range. There are other ways to do this in 1.0 and I may have to do that, but they involve sorting and that could be a problem when showing graphs involving diferrent perspectives. Thanks for your help ahead of time...


Replies (3)

Please register to reply

RE: max() XPath Function in Saxon 8.2 - Added by Anonymous over 19 years ago

Legacy ID: #3014340 Legacy Poster: Michael Kay (mhkay)

The semantics of max() are sensitive to the data type of the argument. tokenize() returns a set of strings; when you supply a set of strings, max() returns the one that is last in alphabetic order (so "99" comes after "100"). If you want the numeric maximum, you need to convert the strings to numbers, which you can do by writing max( for $i in tokenize(...) return number($i) ) Michael Kay

RE: max() XPath Function in Saxon 8.2 - Added by Anonymous over 19 years ago

Legacy ID: #3014536 Legacy Poster: Curtis (cdfisher07)

On page 780 in Appendix B your 2.0 XSLT book says that "Untyped values are compared as numbers.", so if I understand this correctly, these are typed as Strings so they are compared as Strings? In my mind min() and max() are numeric in nature, therefore if any two or n... values can compare they should be compared as numbers. Is there a reason this should not be so? Also, your example is elegant but without your knowlegde I am lost. Dr Kay, with all due respect, I try very hard to use your references and examples, but in many cases I just can't seem to make sense of what seems to be great assistance. Is this written in XPath? I never would have ever come up with it and maybe I should spring for your XPath book as well. I tried your example as: <xsl:variable name="rpm" select="max( for $i in tokenize($data/DataFile/RunData/RPM,'\r?\n') return number($i) )"/> <xsl:value-of select="$rpm"/> and received: NaN as the return. Can you offer an explaination? I tried iterating through the nodes and converting to numbers previously, but was that was not working either. I guess I could do this in a Java extension, but i'm sure it should work in XSLT. Thank you again, and please, I may be venting a bit, but it's very frustrating to not have simple functions work as they would in a procedural language. A declarative language shouldn't be any different for the sake of difference alone. Curtis Fisher

RE: max() XPath Function in Saxon 8.2 - Added by Anonymous over 19 years ago

Legacy ID: #3014622 Legacy Poster: Michael Kay (mhkay)

Untyped values are values whose type is not known. When max() gets untyped values it treats them as numbers, because that's the default type for this function. But the result of tokenize is not untyped - it's sequence of strings, and when max() gets a set of strings, it gives you the one that's last in alphabetic order (more strictly, in collation order). Similarly, if you give it a sequence of dates, it will give you the date that's last in chronological order. Other comparison operations such as sorting or the < and > operators behave in the same polymorphic way. If your function returned NaN, this is because one of the strings in the tokenized sequence couldn't be converted to a number. You can filter out such tokens using the "castable" operator: max( for $i in tokenize(...)[. castable to xs:decimal] return xs:decimal($i) ) Here I've used xs:decimal to convert strings to numbers, rather than number(). xs:decimal doesn't accept floating point numbers such as "1e-02". There's nothing here that's particular to a declarative language. There are plenty of procedural languages that have polymorphic functions and operators. The rules are not that unlike those in JavaScript, for example. However: if you have questions about XSLT and XPath coding or language design that aren't specific to Saxon, the best place to ask them is on xsl-list @ mulberrytech.com Michael Kay

    (1-3/3)

    Please register to reply