I have a list of items, which I want to iterate through and for each iteration, I would like to create a drop down list and then by default select the item based on the current overall index.
The example will make it very clear. Here's the XML:
<?xml version="1.0" encoding="UTF-8"?>
<Plants>
<Plant PlantId="13" PlantType="Tree"/>
<Plant PlantId="25" PlantType="Flower"/>
<Plant PlantId="70" PlantType="Shrub"/>
</Plants>
Then I have some XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:param name="listIdx" select="0">
</xsl:param>
<table>
<thead>
<tr>
<td>PlantType</td>
</tr>
</thead>
<tbody>
<xsl:for-each select="Plants/Plant">
<tr>
<td>
<select>
<xsl:for-each select="/Plants/Plant">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="@PlantId"/>
</xsl:attribute>
<xsl:if test="count(.) = 2">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="@PlantType"/>
</xsl:element>
</xsl:for-each>
</select>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
</xsl:stylesheet>
What I get is this:
PlantType
Tree [=dropdown with Tree, Flower, Shrub]
Tree [=dropdown with Tree, Flower, Shrub]
Tree [=dropdown with Tree, Flower, Shrub]
What I'd love to have is:
PlantType
Tree [=dropdown with Tree, Flower, Shrub (idx 1 preselected)]
Flower [=dropdown with Tree, Flower, Shrub (idx 2 preselected)]
Shrub [=dropdown with Tree, Flower, Shrub (idx 3 preselected)]
I guess there will be two approaches: 1) use a listIdx in the outer loop (match) and then compare the current index within the inner loop with listIdx. 2) compare inner list index with outer list index on the fly.