I have a xml data export and want to transform the data for further reprocessing, xslt 1.0 is demanded. I know the form is a little bit unusual but this is the required form.
I'm quite new to xsl so I would be really thankful for your help.
This is my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<RESULTSET>
<RES>
<NR>1</NR>
<TYPE >XXX </TYPE>
<ITEM>A</ITEM>
<COLOUR>blue</COLOUR>
<MATERIAL>wood</MATERIAL>
<COUNTER>1</COUNTER>
</RES>
<RES>
<NR>2</NR>
<TYPE >YYY </TYPE>
<ITEM>A</ITEM>
<COLOUR>red</COLOUR>
<MATERIAL>plastic</MATERIAL>
<COUNTER>1</COUNTER>
</RES>
<RES>
<NR>2</NR>
<TYPE >YYY </TYPE>
<ITEM>C</ITEM>
<COLOUR>pink</COLOUR>
<MATERIAL>wood</MATERIAL>
<COUNTER>3</COUNTER>
</RES>
<RES>
<NR>3</NR>
<TYPE >ZZZ </TYPE>
<ITEM>C</ITEM>
<COLOUR>yellow</COLOUR>
<MATERIAL>metal</MATERIAL>
<COUNTER>3</COUNTER>
</RES>
<RES>
<NR>1</NR>
<TYPE >XXX</TYPE>
<ITEM>B</ITEM>
<COLOUR>yellow</COLOUR>
<MATERIAL>metal</MATERIAL>
<COUNTER>2</COUNTER>
</RES>
</RESULTSET>
This is my desired output (rows/columns)
NR | TYPE | ITEM-A | COL-A | MAT-A | ITEM-C | COL-C | MAT-C | ITEM-B | COL-B | MAT-B |
---|---|---|---|---|---|---|---|---|---|---|
1 | XXX | A | blue | wood | B | yellow | metal | |||
2 | YYY | A | red | plastic | C | pink | wood | |||
3 | ZZZ | C | yellow | metal |
So I think, I must:
- perform a grouping by Nr
- perform a grouping by Item
- loop through both
What I have until now is this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="res-by-nr" match="RES" use="NR" />
<xsl:template match="RESULTSET">
<th>NR</th>
<th>TYPE</th>
<th>ITEM-A</th>
<th>COL-A</th>
<th>MAT-A</th>
<th>ITEM-B</th>
<th>COL-B</th>
<th>MAT-B</th>
<th>ITEM-C</th>
<th>COL-C</th>
<th>MAT-C</th>
<br></br>
<xsl:for-each select="RES[count(. | key('res-by-nr', NR)[1]) = 1]">
<xsl:sort select="NR" />
<tr>
<td><xsl:value-of select="NR" /></td>
<td><xsl:value-of select="TYPE" /></td>
<xsl:for-each select="key('res-by-nr', NR)">
<xsl:sort select="Counter" />
<xsl:choose>
<xsl:when test = "ITEM='A'">
<td><xsl:value-of select="ITEM"/></td>
<td><xsl:value-of select="COLOUR"/></td>
<td><xsl:value-of select="MATERIAL"/></td>
</xsl:when>
<xsl:otherwise>
<td>Test</td>
<td>Test</td>
<td>Test</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
After this part which goes through the different mapped numbers:
<xsl:for-each select="RES[count(. | key('res-by-nr', NR)[1]) = 1]">
<xsl:sort select="NR" />
<tr>
<td><xsl:value-of select="NR" /></td>
<td><xsl:value-of select="TYPE" /></td>
<xsl:for-each select="key('res-by-nr', NR)">
<xsl:sort select="Counter" />
I have to insert somehow a second grouping which goes through the different Items. I tried different things placing a second key etc. but somehow it doesn't work!
I would appreciate any help or comments. Thank a lot!