finding the minimum value of a set of data

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Porthos

    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>

  • Martin Honnen

    #2
    Re: finding the minimum value of a set of data



    Porthos wrote:

    [color=blue]
    > 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.[/color]
    [color=blue]
    > 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>[/color]

    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

    Comment

    • Jürgen Kahrs

      #3
      Re: finding the minimum value of a set of data

      Porthos wrote:
      [color=blue]
      > 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.[/color]

      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 }

      Comment

      • Dimitre Novatchev

        #4
        Re: finding the minimum value of a set of data


        "Porthos" <jaeaton@vt.edu > wrote in message
        news:1106149204 .938809.163180@ z14g2000cwz.goo glegroups.com.. .[color=blue]
        > 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[/color]

        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"[color=blue]
        >[/color]
        <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.



        [color=blue]
        >
        > 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>
        >[/color]


        Comment

        • jacob.chaney@gmail.com

          #5
          Re: finding the minimum value of a set of data

          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:[color=blue]
          > 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[/color]
          length[color=blue]
          > 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>[/color]

          Comment

          Working...