Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > finding the minimum value of a set of data

Reply
Thread Tools

finding the minimum value of a set of data

 
 
Porthos
Guest
Posts: n/a
 
      01-19-2005
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="September"/>
<month value="October"/>
<month value="November"/>
<month value="December"/>
</calendar>

XSL Document (so far)
=============================
<xsl:template match="/">
<xsl:for-each select="calendar/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">
<xslaram name="min" select="0"/>
<xsl:value-of select="$min"/>

<br/>
</xsl:template>

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-19-2005


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="September"/>
> <month value="October"/>
> <month value="November"/>
> <month value="December"/>
> </calendar>


With the following stylesheet

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

<xslutput method="xml" indent="yes" />

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

<xsl:template name="find-min-string">
<xslaram name="nodes" />
<xslaram 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>
<xsltherwise>
<xsl:value-of select="$head" />
</xsltherwise>
</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="$current-min" />
</xsl:call-template>
</xsl:when>
<xsltherwise>
<xsl:copy-of select="$current-min" />
</xsltherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

the output is

<result>May</result>


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?J=FCrgen_Kahrs?=
Guest
Posts: n/a
 
      01-19-2005
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 }
 
Reply With Quote
 
Dimitre Novatchev
Guest
Posts: n/a
 
      01-19-2005

"Porthos" <> wrote in message
news: oups.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:string-length(),/*/*/@*))]
)


The complete transformation is:

<xsl:stylesheet version="2.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
exclude-result-prefixes="f"
>

<xsl:import href="../f/func-standardXpathFunctions.xsl"/>
<xsl:import href="../f/func-map.xsl"/>

<xslutput method="text"/>

<xsl:template match="/">
<xsl:sequence select=
"data(/*/*/@*
[string-length(.)
=
min(f:map(f:string-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="September"/>
> <month value="October"/>
> <month value="November"/>
> <month value="December"/>
> </calendar>
>
> XSL Document (so far)
> =============================
> <xsl:template match="/">
> <xsl:for-each select="calendar/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">
> <xslaram name="min" select="0"/>
> <xsl:value-of select="$min"/>
>
> <br/>
> </xsl:template>
>



 
Reply With Quote
 
jacob.chaney@gmail.com
Guest
Posts: n/a
 
      01-31-2005
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="September"/>
> <month value="October"/>
> <month value="November"/>
> <month value="December"/>
> </calendar>
>
> XSL Document (so far)
> =============================
> <xsl:template match="/">
> <xsl:for-each select="calendar/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">
> <xslaram name="min" select="0"/>
> <xsl:value-of select="$min"/>
>
> <br/>
> </xsl:template>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stl map: get the <key,value> pair which has the minimum value Rui Maciel C++ 2 12-01-2009 11:21 PM
strange outcome when comparing int value against a give minimum and maximum value Pugi! Javascript 2 02-12-2007 09:10 PM
Set Minimum Date Calendar Control Joey ASP .Net 2 03-15-2006 07:51 PM
Minimum feature set for MLFR? you know who maybe Cisco 1 03-02-2006 10:59 PM
finding the minimum allowed value for a type Adam H. Peterson C++ 17 09-29-2003 07:03 PM



Advertisments