I am trying to print an average with xslt and having a hard time trying to figure out a way. I have answers in a separate node and questions in a separate node. Need to match the question id with answer and calculate average
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<details>
<detail>
<answersall>
<answers>
<question id="1"/>
<answer>4</answer>
<note>test</note>
</answers>
<answers>
<question id="2"/>
<answer>2</answer>
</answers>
<answers>
<question id="3"/>
<answer>2</answer>
</answers>
<answers>
<question id="4"/>
<answer>3</answer>
</answers>
</answerall>
</detail>
<questions>
<question id="1" text="Hello how are you" section="a"/>
<question id="2" text="how was your day" section="a"/>
<question id="3" text="it was good" section="b"/>
<question id="4" text="take care" section="b"/>
</questions>
</details>
I am trying to print something like this
Question Answer
Section A 3 (average)
1. Hello how are you 4
2.How was your day 2
Section B 2.5
3. It was good 2
4. Take Care 3
I have got the part to print the questions and answers but not the average. I know i cannot use a variable inside a for loop.
Below is my xslt.
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2><center>Details</center></h2>
<table>
<tr>
<td>Section A</td>
<td>????</td>
</tr>
<xsl:for-each select="details/questions/question[@section='a']">
<xsl:variable name="id" select="@id"/>
<tr>
<td><xsl:value-of select="@text"/></td>
<td><xsl:value-of select="//answers[question/@id=$id]/answer"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>