我有一个项目列表,我想迭代一遍,对于每一次迭代,我想创建一个下拉列表,然后默认情况下根据当前的总体索引选择项目。
这个例子将非常清楚。以下是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>然后我有一些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>我得到的是:
PlantType 树=与树、花、灌木一起下垂 树=与树、花、灌木一起下垂 树=与树、花、灌木一起下垂
我想要的是:
PlantType 树=有树、花、灌木的下拉(idx 1预选) 花=树、花、灌木下垂(idx 2预选) 灌木=树、花、灌木的下拉(idx 3预选)
我想有两种方法: 1)在外部循环(match)中使用listIdx,然后将内环中的当前索引与listIdx进行比较。2)动态比较内表指标和外表指标。
发布于 2012-12-10 08:52:05
您可以做的是在外部循环中定义一个变量,以保存工厂元素的当前位置。
<xsl:variable name="position" select="position()"/>然后,在内环中,您可以根据这个变量检查第二个位置,该变量仍在作用域中。
<xsl:if test="position() = $position">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>下面是本例中完整的XSLT
<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"/>
<table>
<thead>
<tr>
<td>PlantType</td>
</tr>
</thead>
<tbody>
<xsl:for-each select="Plants/Plant">
<xsl:variable name="position" select="position()"/>
<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="position() = $position">
<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>这将产生以下输出
<table>
<thead>
<tr>
<td>PlantType</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<select>
<option value="13" selected="selected">Tree</option>
<option value="25">Flower</option>
<option value="70">Shrub</option>
</select>
</td>
</tr>
<tr>
<td>
<select>
<option value="13">Tree</option>
<option value="25" selected="selected">Flower</option>
<option value="70">Shrub</option>
</select>
</td>
</tr>
<tr>
<td>
<select>
<option value="13">Tree</option>
<option value="25">Flower</option>
<option value="70" selected="selected">Shrub</option>
</select>
</td>
</tr>
</tbody>
</table>但是,如果只是为了避免过多的缩进,通常最好使用xsl:apply-templates而不是xsl:for-。在这种情况下,您还可以将该位置作为参数传递。下面的XSLT还生成相同的输出
<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"/>
<table>
<thead>
<tr>
<td>PlantType</td>
</tr>
</thead>
<tbody>
<xsl:apply-templates select="Plants/Plant"/>
</tbody>
</table>
</xsl:template>
<xsl:template match="Plant">
<tr>
<td>
<select>
<xsl:apply-templates select="/Plants/Plant" mode="options">
<xsl:with-param name="position" select="position()"/>
</xsl:apply-templates>
</select>
</td>
</tr>
</xsl:template>
<xsl:template match="Plant" mode="options">
<xsl:param name="position"/>
<option value="{@PlantId}">
<xsl:if test="position() = $position">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="@PlantType"/>
</option>
</xsl:template>
</xsl:stylesheet>还请注意使用属性值模板在选项元素上创建值属性(并注意没有真正需要使用xsl: element 来创建静态名称元素)
https://stackoverflow.com/questions/13794852
复制相似问题