I am using XSLT 1.0
I transforme the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Features>
<Feature style="a" item="a1" area="1"></Feature>
<Feature style="a" item="a1" area="1"></Feature>
<Feature style="a" item="a2" area="1"></Feature>
<Feature style="b" item="b1" area="1"></Feature>
<Feature style="b" item="b1" area="1"></Feature>
<Feature style="b" item="b2" area="1"></Feature>
</Features>
via this xsl file:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="KeyStyle" match="Feature" use="@style"/>
<xsl:key name="KeyItem" match="Feature" use="@item"/>
<xsl:template match="Features">
<html>
<xsl:for-each select="Feature[generate-id() = generate-id(key('KeyStyle',@style)[1])]">
<xsl:sort select="@style" order="ascending"/>
Table for style (<xsl:value-of select="@style"/>)
<xsl:for-each select="key('KeyStyle',@style)">
<xsl:sort select="@item" order="ascending"/>
<xsl:if test="not(preceding::*[@item = current()/@item])">
<p>Style=<xsl:value-of select="@style"/></p>
<p>Item=<xsl:value-of select="@item"/></p>
<p>TotalAreaOf_Items (<xsl:value-of select="@item"/>)=<xsl:value-of select="sum(key('KeyItem',@item)/@area)"/></p>
</xsl:if>
</xsl:for-each> ----------
<br/><br/>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
Getting this output:
<html>
Table for style (a)
<p>Style=a</p>
<p>Item=a1</p>
<p>TotalAreaOf_Items (a1)=2</p>
<p>Style=a</p>
<p>Item=a2</p>
<p>TotalAreaOf_Items (a2)=2</p><br><br>
-----------------
Table for style (b)
<p>Style=b</p>
<p>Item=b1</p>
<p>TotalAreaOf_Items (b1)=2</p>
<p>Style=b</p>
<p>Item=b2</p>
<p>TotalAreaOf_Items (b2)=1</p><br><br>
</html>
Easy up to here. But now I try to transform the following new xml getting exactly the same output as in my previous example:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Features>
<Feature item="a1" area="1"></Feature>
<Feature item="a1" area="1"></Feature>
<Feature item="a2" area="1"></Feature>
<Feature item="a2" area="1"></Feature>
<Feature item="b1" area="1"></Feature>
<Feature item="b1" area="1"></Feature>
<Feature item="b2" area="1"></Feature>
</Features>
which new xml lacks of @style attributes, but the @item’s first letter is the same as the @style attribute of the previous example.
The plan is to scan by the first letter of each @item attribute instead of the previous @style attribute.
The first idea was to use something like [starts-with (@item,’a’)] etc, but what if I have xmls with various @item’s first letter.
Please for any advise.