Could you please help me in developing a xsl code that could do the following activities sequentially:
1. take average of two elements
2. normalize the average value across records.
Where I am stumped in the development of xsl is the reuse of a newly added for the next element.
Any help would be appreciated
Example:
In the sample input file below for each record
1. avg = (c + d)/2
- avg = (c+d)/2 = (12+12)/2 = 12, (8+12)/2=10 ....
2. avg_nom = avg/min(avg)
- avg_nom = avg/min(avg) = 12/min(12,10,15,27)=1.2
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<c>12</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<c>8</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<c>10</c>
<d>20</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<c>20</c>
<d>34</d>
</Results>
</top>
The expected result should be
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<avg>12</avg>
<avg_nom>1.2</avg_nom>
<c>12</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<avg>10</avg>
<avg_nom>1</avg_nom>
<c>8</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<avg>15</avg>
<avg_nom>1.5</avg_nom>
<c>10</c>
<d>20</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<avg>27</avg>
<avg_nom>2.7</avg_nom>
<c>20</c>
<d>34</d>
</Results>
</top>
With XSLT 2.0 you can use
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="step1-result">
<xsl:apply-templates select="top" mode="step1"/>
</xsl:variable>
<xsl:template match="@* | node()" mode="#all">
<xsl:copy>
<xsl:apply-templates select="@* , node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="c" mode="step1">
<avg><xsl:value-of select="(. + ../d) div 2"/></avg>
<xsl:next-match/>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:variable name="min-avg" select="min($step1-result/top/Results/avg)"/>
<xsl:apply-templates select="$step1-result/top/*">
<xsl:with-param name="min-avg" tunnel="yes" select="$min-avg"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="avg">
<xsl:param name="min-avg" tunnel="yes"/>
<xsl:next-match/>
<avg_nom><xsl:value-of select=". div $min-avg"/></avg_nom>
</xsl:template>
</xsl:stylesheet>
Here is an XSLT 1.0 version of above stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="step1-result-fragment">
<xsl:apply-templates select="top" mode="step1"/>
</xsl:variable>
<xsl:variable name="step1-result" select="exsl:node-set($step1-result-fragment)"/>
<xsl:template match="@* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | node()" mode="step1" name="step1-identity">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="step1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="c" mode="step1">
<avg><xsl:value-of select="(. + ../d) div 2"/></avg>
<xsl:call-template name="step1-identity"/>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:variable name="min-avg">
<xsl:for-each select="$step1-result/top/Results/avg">
<xsl:sort select="." data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:apply-templates select="$step1-result/top/Results">
<xsl:with-param name="min-avg" select="$min-avg"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="Results">
<xsl:param name="min-avg"/>
<xsl:copy>
<xsl:apply-templates select="@* | node()">
<xsl:with-param name="min-avg" select="$min-avg"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="avg">
<xsl:param name="min-avg"/>
<xsl:call-template name="identity"/>
<avg_nom><xsl:value-of select=". div $min-avg"/></avg_nom>
</xsl:template>
</xsl:stylesheet>