Grouping elements by attribute values - XSLT 1.0



I am trying to apply this answer to my case but I am not succeeding.


What I want to do is group elements with same position under another element like so:


input XML:



<ROOT>
<ROW row_num="1" row_name="firstRow">
<CELL position="1" id="515" price="15"/>
<CELL position="2" id="466" price="22"/>
<CELL position="3" id="539" price="4"/>
</ROW>
<ROW row_num="2" row_name="secondRow">
<CELL position="1" id="1005" price="33.2"/>
<CELL position="1" id="518" price="12.5"/>
<CELL position="2" id="539" price="17"/>
<CELL position="2" id="1016" price="19"/>
</ROW>
<ROW row_num="3" row_name="thirdRow">
<CELL position="1" id="539" price="13"/>
<CELL position="1" id="1150" price="34"/>
<CELL position="1" id="458" price="18.2"/>
</ROW>
</ROOT>


output HTML:



<TABLE>
<TR>
<TD>1</TD>
<TD>firstRow</TD>
<TD>
<DIV>515</DIV>
</TD>
<TD>
<DIV>15</DIV>
</TD>
<TD>
<DIV>466</DIV>
</TD>
<TD>
<DIV>22</DIV>
</TD>
<TD>
<DIV>539</DIV>
</TD>
<TD>
<DIV>4</DIV>
</TD>
</TR>
<TR>
<TD>2</TD>
<TD>secondRow</TD>
<TD>
<DIV>1005</DIV>
<DIV>518</DIV>
</TD>
<TD>
<DIV>33.2</DIV>
<DIV>12.5</DIV>
</TD>
<TD>
<DIV>539</DIV>
<DIV>1016</DIV>
</TD>
<TD>
<DIV>17</DIV>
<DIV>19</DIV>
</TD>
</TR>
<TR>
<TD>3</TD>
<TD>thirdRow</TD>
<TD>
<DIV>539</DIV>
<DIV>1150</DIV>
<DIV>458</DIV>
</TD>
<TD>
<DIV>13</DIV>
<DIV>34</DIV>
<DIV>18.2</DIV>
</TD>
</TR>
</TABLE>


So I'd like a table with some leading metadata and all CELL's within one row and same position to be grouped by position into a TD and different. Same for other attributes. Number of CELLs within ROW can differ, so can number of CELLs with same position number. CELLs are always ordered, position number always starts with 1 within a ROW.


This is how for I got, but it is not grupped:



<xsl:stylesheet version="1.0"
xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output method="html" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="ROOT">
<table>
<xsl:apply-templates select="ROW"/>
</table>
</xsl:template>
<xsl:template match="ROW">
<tr>
<td>
<xsl:value-of select="@row_num" />
</td>
<td>
<xsl:value-of select="@row_name" />
</td>
<xsl:apply-templates select="CELL"/>
</tr>
</xsl:template>
<xsl:template match="CELL">
<td>
<div>
<xsl:value-of select="@id" />
</div>
</td>
<td>
<div>
<xsl:value-of select="@price" />
</div>
</td>
</xsl:template>
</xsl:stylesheet>


check my xsltcake here.


No comments:

Post a Comment