在xslt下面用于绑定值以下拉,但是值仍然存在于仍然显示为“选择”的所选值中的“值”属性中。
<xsl:for-each select="./Fields/Field[@id='State']">
<select req="{./@req}" name="leaveform" misstext="Please Select" fieldid="{./@id}" id="txt{./@id}" style="width:100px;" boundProp="value" >
<xsl:attribute name="value">
<xsl:value-of select="./@fieldValue" />
</xsl:attribute>
<option value="">--Select--</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">
<!--<xsl:if test="GA">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>-->
Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
</select>
</xsl:for-each>
答案 0 :(得分:0)
您的样式表正在为value
元素创建select
属性,这在HTML中不正确。
您需要创建的内容可能是这样的,选项的属性selected="selected"
具有该字段的当前值:
<强>输出强>
<select req="reqvalue" name="leaveform" misstext="Please Select" fieldid="State" id="txtState" style="width:100px;" boundProp="value">
<option value="">--Select--</option>
<option value="AL">Alabama</option>
<option value="AK" selected="selected">Alaska</option>
<option value="AZ">Arizona</option>
...
</select>
因此,从您的XSLT我们可以假设您的输入包含这样的内容:
<强>输入强>
...
<Fields>
<Field id="State" req="reqvalue" fieldValue="KS"/>
</Fields>
...
如果只能使用XSLT 1.0,则需要一个包含不同选项的xml文件:
<强> select.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<select>
<option value="">--Select--</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
....
<option value="ME">Maine</option>
</select>
这是样式表:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Field[@id='State']">
<!-- get the current value of the field -->
<xsl:variable name="value">
<xsl:value-of select="@fieldValue" />
</xsl:variable>
<!-- get the different options -->
<xsl:variable name="states" select="document('select.xml')"/>
<!-- build the result -->
<select req="{@req}" name="leaveform" misstext="Please Select" fieldid="{@id}" id="txt{@id}" style="width:100px;" boundProp="value" >
<xsl:apply-templates select="$states//option">
<xsl:with-param name="selectedValue" select="$value"/>
</xsl:apply-templates>
</select>
</xsl:template>
<xsl:template match="option">
<xsl:param name="selectedValue"/>
<xsl:copy>
<xsl:copy-of select="@value"/>
<!-- add the selected attribute to the option
with the current value -->
<xsl:if test="@value = $selectedValue">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
单个文件变体
如果您可以使用XSLT 2.0,或者您的XSLT 1.0处理器支持返回样式表树的document('')
函数调用,而不是使用外部xml文件,则可以直接在内部使用不同的option
元素变量:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- variable with the different options -->
<xsl:variable name="options">
<option value="">--Select--</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
</xsl:variable>
<xsl:template match="Field[@id='State']">
<!-- get the current value of the field -->
<xsl:variable name="value">
<xsl:value-of select="@fieldValue" />
</xsl:variable>
<!-- build the result -->
<select req="{@req}" name="leaveform" misstext="Please Select" fieldid="{@id}" id="txt{@id}" style="width:100px;" boundProp="value" >
<!-- with XSLT 2.0 you can select simply "$options/option" -->
<xsl:apply-templates select="document('')//xsl:variable[@name = 'options']/option">
<xsl:with-param name="selectedValue" select="$value"/>
</xsl:apply-templates>
</select>
</xsl:template>
<xsl:template match="option">
<xsl:param name="selectedValue"/>
<option value="{@value}">
<!-- add the selected attribute to the option
with the current value -->
<xsl:if test="@value = $selectedValue">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</option>
</xsl:template>
</xsl:stylesheet>