Reputation: 845
In XSL I need to generate HTML tags depending on what iteration of the for-each i am..
For example, if I have the following XML:
<Items>
<Car>Mercedes</Car>
<Bike>Gt</Bike>
<House>123</House>
<Skate>111</Skate>
<Plane>5522</Plane>
<tv>Sony</tv>
</Items>
And with my XSL y need to generate the HTML displaying 2 items for each row. I need to display only 4 tds per row, 2 of them with the title and the 2 others with the value. With my previous example the expected HTML would be:
<table>
<tr>
<td>Car</td>
<td>Mercedes</td>
<td>Bike</td>
<td>Gt</td>
</tr>
<tr>
<td>House</td>
<td>123</td>
<td>Skate</td>
<td>111</td>
</tr>
<tr>
<td>Plane</td>
<td>5522</td>
<td>tv</td>
<td>Sony</td>
</tr>
</table>
I,ve tried with when/otherwise inside the for-each, opening or closing the <tr>
, but the XSL is invalid..
Is there a way to achieve this??
Upvotes: 0
Views: 131
Reputation: 56202
Use:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Items">
<table>
<xsl:for-each select="*[position() mod 2 != 0]">
<tr>
<td>
<xsl:value-of select="name()"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="name(following-sibling::*)"/>
</td>
<td>
<xsl:value-of select="following-sibling::*"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output:
<table>
<tr>
<td>Car</td>
<td>Mercedes</td>
<td>Bike</td>
<td>Gt</td>
</tr>
<tr>
<td>House</td>
<td>123</td>
<td>Skate</td>
<td>111</td>
</tr>
<tr>
<td>Plane</td>
<td>5522</td>
<td>tv</td>
<td>Sony</td>
</tr>
</table>
Upvotes: 1