Jump to content

xslt with an xml files with diffrent tags


ingenuous67

Recommended Posts

hello i have an xml file that contain : <students> <stu> <name>ali darrudi</name> <lessons> <web>20</web> <compiler>18</compiler> <projeh>19</projeh> </lessons> <avg></avg> </stu> <stu> <name>mahmood abolfathzadeh</name> <lessons> <graphic>10</graphic> <compiler>10</compiler> <projeh>12</projeh> <simulation>12</simulation> </lessons> <avg></avg> </stu></students> i want to show average of each student with xslt.note : count of lessons of each students is variable.my mail : ali.daroudi@gmail.com

Link to comment
Share on other sites

I would use a recursive template but I'd love to see how others might do this.

<xsl:template match="avg"/><xsl:template name="findAverage">  <xsl:param name="numlessons"/>  <xsl:param name="pointer"/>  <xsl:param name="total"/>  <xsl:choose>   <xsl:when test="$pointer <= $numlessons">    <xsl:variable name="subtotal">	 <xsl:value-of select="child::*[number($pointer)]"/>    </xsl:variable>    <xsl:variable name="total">	 <xsl:value-of select="number($subtotal) + number($total)"/>    </xsl:variable>    <xsl:call-template name="findAverage">	 <xsl:with-param name="numlessons" select="$numlessons"/>	 <xsl:with-param name="pointer" select="$pointer + 1"/>	 <xsl:with-param name="total" select="$total"/>    </xsl:call-template>   </xsl:when>   <xsl:otherwise>    <!-- Divide total by numlessons -->    <xsl:value-of select="$total div $numlessons"/>   </xsl:otherwise>  </xsl:choose></xsl:template><xsl:template match="lessons">  <xsl:variable name="numlessons">   <xsl:value-of select="count(child::*)"/>  </xsl:variable>  <xsl:variable name="xaverage">   <xsl:call-template name="findAverage">    <xsl:with-param name="numlessons" select="$numlessons"/>    <xsl:with-param name="pointer" select="1"/>    <xsl:with-param name="total" select="0"/>   </xsl:call-template>  </xsl:variable>  <xsl:element name="lessons">   <xsl:apply-templates/>  </xsl:element>  <xsl:text></xsl:text>  <xsl:element name="avg">   <xsl:value-of select="$xaverage"/>  </xsl:element></xsl:template>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...