首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据-每个索引在select中设置html选项元素的选定属性

根据-每个索引在select中设置html选项元素的选定属性
EN

Stack Overflow用户
提问于 2012-12-10 04:07:30
回答 1查看 12.8K关注 0票数 0

我有一个项目列表,我想迭代一遍,对于每一次迭代,我想创建一个下拉列表,然后默认情况下根据当前的总体索引选择项目。

这个例子将非常清楚。以下是XML:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
<?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)动态比较内表指标和外表指标。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-10 08:52:05

您可以做的是在外部循环中定义一个变量,以保存工厂元素的当前位置。

代码语言:javascript
复制
 <xsl:variable name="position" select="position()"/>

然后,在内环中,您可以根据这个变量检查第二个位置,该变量仍在作用域中。

代码语言:javascript
复制
<xsl:if test="position() = $position">
    <xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>

下面是本例中完整的XSLT

代码语言:javascript
复制
<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>

这将产生以下输出

代码语言:javascript
复制
<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还生成相同的输出

代码语言:javascript
复制
<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 来创建静态名称元素)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13794852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档