[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Extracting highest values within a range


Subject: Re: [xsl] Extracting highest values within a range
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 14 Jan 2005 17:11:21 GMT

This would be easier in xslt2 or with node-set extension or if you
just gave the range as numerical values rather than prefixed with
"Result" but anyway:


<x>

<params>
	<resultstocompare>
		<start>Result1</start>
		<end>Result3</end>
	</resultstocompare>
	<resultstocompare>
		<start>Result5</start>
		<end>Result6</end>
	</resultstocompare>
</params>

<wrapper>
	<column1>
		<name>Result1</name>
		<name>Result2</name>
		<name>Result3</name>
		<name>Result4</name>
		<name>Result5</name>
		<name>Result6</name>
	</column1>
	<column2>
		<amount>1000</amount>
		<amount>200</amount>
		<amount>3000</amount>
		<amount>40</amount>
		<amount>50</amount>
		<amount>11</amount>
	</column2>
	<column3>
		<amount>44</amount>
		<amount>800</amount>
		<amount>66</amount>
		<amount>120</amount>
		<amount>90</amount>
		<amount>101</amount>
	</column3>
	<column4>
		<amount>1000</amount>
		<amount>200</amount>
		<amount>3000</amount>
		<amount>40</amount>
		<amount>50</amount>
		<amount>11</amount>
	</column4>
	<column5>
		<amount>1000</amount>
		<amount>200</amount>
		<amount>3000</amount>
		<amount>40</amount>
		<amount>50</amount>
		<amount>11</amount>
	</column5>
</wrapper>

</x>





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

<xsl:output indent="yes"/>

<xsl:template match="x">
 <xsl:apply-templates select="wrapper"/>
</xsl:template>

<xsl:template match="wrapper|wrapper/*">
<xsl:copy>
 <xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="name">
 <xsl:variable name="n" select="substring-after(.,'Result')"/>
 <xsl:choose>
  <xsl:when test="/x/params/resultstocompare/start[substring-after(.,'Result')=$n]">
  <name>
   <xsl:value-of select="."/> - <xsl:value-of select="/x/params/resultstocompare/start[substring-after(.,'Result')=$n]/following-sibling::end"/>
  </name>
  </xsl:when>
  <xsl:when test="/x/params/resultstocompare[substring-after(start,'Result') &lt;$n]
                                            [substring-after(end,'Result') &gt;=$n]
              "/>
  <xsl:otherwise>
  <xsl:copy-of select="."/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="amount">
<xsl:variable name="n" select="position()"/>
 <xsl:choose>
  <xsl:when test="/x/params/resultstocompare/start[substring-after(.,'Result')=$n]">
  <ammount>
   <xsl:variable name="e" 
     select="substring-after(/x/params/resultstocompare/start[substring-after(.,'Result')=$n]/following-sibling::end,'Result')"/>
   <xsl:for-each select=".|following-sibling::amount[position() &lt;= $e - $n]">
   <xsl:sort data-type="number"/>
   <xsl:if test="position()=last()">
   <xsl:value-of select="."/>
   </xsl:if>
  </xsl:for-each>
  </ammount>
  </xsl:when>
  <xsl:when test="/x/params/resultstocompare[substring-after(start,'Result') &lt;$n]
                                            [substring-after(end,'Result') &gt;=$n]
              "/>
  <xsl:otherwise>
  <xsl:copy-of select="."/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>




$ java -jar saxon8.jar  res.xml res.xsl
Warning: Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor
<?xml version="1.0" encoding="UTF-8"?>
<wrapper>
   <column1>
      <name>Result1 - Result3</name>
      <name>Result4</name>
      <name>Result5 - Result6</name>
   </column1>
   <column2>
      <ammount>3000</ammount>
      <amount>40</amount>
      <ammount>50</ammount>
   </column2>
   <column3>
      <ammount>800</ammount>
      <amount>120</amount>
      <ammount>101</ammount>
   </column3>
   <column4>
      <ammount>3000</ammount>
      <amount>40</amount>
      <ammount>50</ammount>
   </column4>
   <column5>
      <ammount>3000</ammount>
      <amount>40</amount>
      <ammount>50</ammount>
   </column5>
</wrapper>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________


Current Thread
Keywords