473,803 Members | 3,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding the minimum value of a set of data

Hi All,

I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one.

This would be simple if I could re-assign values to a variable,
but from what I gather I can't do that. How do I keep track of the
lowest value as I loop through? My XSL document only finds the length
of each string and prints it out (for now). I can write a template
that calls itself for recursion, but I don't know how to keep the
minimum value readially available as I go through each loop.

Thanks,

James

XML Document
=============== ==============
<calendar name="americana ">
<month value="January"/>
<month value="February "/>
<month value="March"/>
<month value="April"/>
<month value="May"/>
<month value="June"/>
<month value="July"/>
<month value="August"/>
<month value="Septembe r"/>
<month value="October"/>
<month value="November "/>
<month value="December "/>
</calendar>

XSL Document (so far)
=============== ==============
<xsl:template match="/">
<xsl:for-each select="calenda r/month">
<xsl:call-template name="find_min_ max">
<xsl:with-param name="min" select="string-length(@value)"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="find_min_ max">
<xsl:param name="min" select="0"/>
<xsl:value-of select="$min"/>

<br/>
</xsl:template>

Jul 20 '05 #1
4 4730


Porthos wrote:

I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one. XML Document
=============== ==============
<calendar name="americana ">
<month value="January"/>
<month value="February "/>
<month value="March"/>
<month value="April"/>
<month value="May"/>
<month value="June"/>
<month value="July"/>
<month value="August"/>
<month value="Septembe r"/>
<month value="October"/>
<month value="November "/>
<month value="December "/>
</calendar>


With the following stylesheet

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

<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
<result>
<xsl:call-template name="find-min-string">
<xsl:with-param name="nodes" select="calenda r/month/@value" />
</xsl:call-template>
</result>
</xsl:template>

<xsl:template name="find-min-string">
<xsl:param name="nodes" />
<xsl:param name="min" select="''" />
<xsl:variable name="head" select="$nodes[1]" />
<xsl:variable name="tail" select="$nodes[position() &gt; 1]" />
<xsl:variable name="current-min">
<xsl:choose>
<xsl:when test="$min != '' and string-length($min) &lt;
string-length($head)">
<xsl:value-of select="$min" />
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$head" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:choose>
<xsl:when test="$tail">
<xsl:call-template name="find-min-string">
<xsl:with-param name="nodes" select="$tail" />
<xsl:with-param name="min" select="$curren t-min" />
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:copy-of select="$curren t-min" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

the output is

<result>May</result>
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
Porthos wrote:
I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one.


If your intention really is to print the minimum
value, then it is not necessary to use XSL. Any
other tool like XMLgawl will also do. This one
is tested and works:

BEGIN { XMLMODE=1; OFS= "\t" }

XMLSTARTELEM == "month" {
# Initialize shortest
if (shortest == "")
shortest = XMLATTR["value"]
# Find shortest value
if (length(XMLATTR["value"]) < length(shortest ))
shortest = XMLATTR["value"]
}

END { print shortest }
Jul 20 '05 #3

"Porthos" <ja*****@vt.edu > wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hi All,

I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one.

This would be simple if I could re-assign values to a variable,
but from what I gather I can't do that. How do I keep track of the
lowest value as I loop through? My XSL document only finds the length
of each string and prints it out (for now). I can write a template
that calls itself for recursion, but I don't know how to keep the
minimum value readially available as I go through each loop.

Thanks,

James
In XSLT 2.0 + FXSL the result is obtained by the following one-liner:

data(/*/*/@*
[string-length(.)
=
min(f:map(f:str ing-length(),/*/*/@*))]
)
The complete transformation is:

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f" <xsl:import href="../f/func-standardXpathFu nctions.xsl"/>
<xsl:import href="../f/func-map.xsl"/>

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:sequence select=
"data(/*/*/@*
[string-length(.)
=
min(f:map(f:str ing-length(),/*/*/@*))]
)"
/>
</xsl:template>
</xsl:stylesheet>
When applied on your source xml document, this transformation produces the
wanted result:

May
It also will produce the list of all minimal values if there is more than
one minimal value.
Cheers,

Dimitre Novatchev.


XML Document
=============== ==============
<calendar name="americana ">
<month value="January"/>
<month value="February "/>
<month value="March"/>
<month value="April"/>
<month value="May"/>
<month value="June"/>
<month value="July"/>
<month value="August"/>
<month value="Septembe r"/>
<month value="October"/>
<month value="November "/>
<month value="December "/>
</calendar>

XSL Document (so far)
=============== ==============
<xsl:template match="/">
<xsl:for-each select="calenda r/month">
<xsl:call-template name="find_min_ max">
<xsl:with-param name="min" select="string-length(@value)"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="find_min_ max">
<xsl:param name="min" select="0"/>
<xsl:value-of select="$min"/>

<br/>
</xsl:template>

Jul 20 '05 #4
James,

This is an off topic response to your post but I too am looking into
the CycleTrak motorcyle tracking system and saw your post on google
groups. I was wondering if you ever purchased it or had any experience
with it that you would care to share with me? Thank you in advance.
Jacob
Porthos wrote:
Hi All,

I'm trying to find the minimum value of a set of data (see below).
I want to compare the lengths of these attribute values and display
the lowest one.

This would be simple if I could re-assign values to a variable,
but from what I gather I can't do that. How do I keep track of the
lowest value as I loop through? My XSL document only finds the length of each string and prints it out (for now). I can write a template
that calls itself for recursion, but I don't know how to keep the
minimum value readially available as I go through each loop.

Thanks,

James

XML Document
=============== ==============
<calendar name="americana ">
<month value="January"/>
<month value="February "/>
<month value="March"/>
<month value="April"/>
<month value="May"/>
<month value="June"/>
<month value="July"/>
<month value="August"/>
<month value="Septembe r"/>
<month value="October"/>
<month value="November "/>
<month value="December "/>
</calendar>

XSL Document (so far)
=============== ==============
<xsl:template match="/">
<xsl:for-each select="calenda r/month">
<xsl:call-template name="find_min_ max">
<xsl:with-param name="min" select="string-length(@value)"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name="find_min_ max">
<xsl:param name="min" select="0"/>
<xsl:value-of select="$min"/>

<br/>
</xsl:template>


Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5376
by: Adam H. Peterson | last post by:
Is there a standard way to find the minimum value for a data type? I'm thinking along the lines of std::numeric_limits<T>::min(). But that doesn't work for floating point types. I can't use (-std::numeric_limits<T>::max()) either, because that won't work for unsigned types (and technically won't work for signed integer types and others either, although it would probably be "good enough" for them). Do I need to implement a new template...
4
2053
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally. Bigint math isn't much slower, for integers that all fit within an integer. 2) Converting float to varchar is relatively slow, and should be avoided if possible. Converting from integer to varchar or varchar to int is several times faster.
2
2337
by: Hennie de Nooijer | last post by:
Because of an error in google or underlying site i can reply on my own issue. Therefore i copied the former entered message in this message. -------------------------------------REPY---------------------------------- Hi Maybe i wasn't clear. I want to dynamically check whether what the lowest date and the highest date is in the calendar table. The presented solutions has fixed dates and i don't want that. If i could store a global...
3
2765
by: D Denholm | last post by:
I am a Access newbie... Hopefully somebody can help me figure this out. I have a database that looks like: Asset Economic Minimum ----- ---------------- 10555 320 10555 320 10555 320
2
2274
by: slickn_sly | last post by:
int find_index_of_min( float num, int arraySize ) { int index, min; min = 0; for( index = 1; index < arraySize; index++ ) { if( num > num ) { min = index;
1
11965
by: Dan | last post by:
I have the following code that executes a parameterized Query and pupulates the data in a datagrid. All is good. My problem is I would now like to get the minimum and maximum value of one of the columns. My code that works is: public void button2_Click(object sender, System.EventArgs e) { dataSet21.Clear(); this.oleDbSelectCommand2.Parameters.Value = textBox2.Text; int myEndKey = int.Parse(textBox2.Text);...
4
25185
by: Nhd | last post by:
Write a program that reads daily temperatures, as floats. Read in a loop until an EOF. After the input has been read, print the maximum and minimum values. You can assume that there is at least one input data value. Sample input values 10.0 11.3 4.5 -2.0 3.6 -3.3 0.0 The output should look something like the following. Formatting may vary. Maximum = 11.3 Minimum = -3.3
5
6330
by: davenet | last post by:
Hi, I'm new to Python and working on a school assignment. I have setup a dictionary where the keys point to an object. Each object has two member variables. I need to find the smallest value contained in this group of objects. The objects are defined as follows:
1
2626
by: hello12 | last post by:
Hello, I was asked to write a C program to find minimum normalized positive number that can be represented in my computer. I know that the value is 2.2250738585072014e-308. I wrote the program to find the machine precision. Now i need to make some parameter changes to this program to find the min. number given above. do { Epsilon = Epsilon/2.0 ; //Variable value is halved until the smallest //value of 'e' is...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.