RE: [xsl] Average result out of scope to then sort elements

Subject: RE: [xsl] Average result out of scope to then sort elements
From: bryan.s.schnabel@xxxxxxxxxxxxxx
Date: Wed, 21 Jan 2004 13:13:14 -0800
Adjusting your XML so it has extra samples, and it's well formed:

<xes>
 <x id="1">
   <y>
     <z price="32" />
     <z price="45" />
     <z price="67" />
   </y>
</x>
<x id="1.1">
   <y>
     <z price="3" />
     <z price="4" />
     <z price="7" />
   </y>
</x>
<x id="2">
   <y>
     <z price="36" />
     <z price="46" />
     <z price="37" />
   </y>
</x>
</xes>

This XSL should sort the way you specify:

<xsl:template match="node()|@*">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="xes">
 <xsl:for-each select="x">
  <xsl:sort select="sum(y/z/@price) 
                    div count(y/z/@price)" 
            data-type="number"/>
  <x rank="{format-number(sum(y/z/@price) 
            div count(y/z/@price),'#.00')}"
      id="{@id}">
   <xsl:apply-templates />
  </x>
 </xsl:for-each>
</xsl:template>

And result in this:

<x rank="4.67" id="1.1">
   <y>
      <z price="3"/>
      <z price="4"/>
      <z price="7"/>
   </y>
</x>
<x rank="39.67" id="2">
   <y>
      <z price="36"/>
      <z price="46"/>
      <z price="37"/>
   </y>
</x>
<x rank="48.00" id="1">
   <y>
      <z price="32"/>
      <z price="45"/>
      <z price="67"/>
   </y>
</x>


-----Original Message-----
From: Fran [mailto:franciscojose@xxxxxxxxxxx] 
Sent: Wednesday, January 21, 2004 7:20 AM
To: XSL List (E-mail)
Subject: [xsl] Average result out of scope to then sort elements


 Hi list, I've a problem and I can't find a good solution. I try to paint
all "x" elements sorting by their average result of all their "z" childrens
but a have the variable out of scope. I know that I can't utilize variables
out of their scope(for each loop)  in my XSL but I don't know how do this.
I do this with this XML:
     XML
 <x id="1">
   <y>
     <z price="32">
     <z price="45">
     <z price="67">
   <y>
</x>
<x id="2">
   <y>
     <z price="36">
     <z price="46">
     <z price="37">
   <y>
</x>

  <XSL>
   ......
      <xsl:for-each select="X">

		<xsl:for-each  select="y">
                       <!-- I calculate here the average of their
children-->
                          <xsl:variable name="resultz"
select='format-number(sum(z/@preciouni) div count(z/@preciouni), "#.00")' />
		</xsl:for-each>

           <xsl:sort select="$resultz" data-type="number"
order="descending"/> <!--Sort x elements by their average-->

         <xsl:value-of select="@id"/>
         <xsl:value-of select="$resultz"/>
     </xsl:for-each>

Any suggestion, please?
Regards
Frank



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread