我试图在XSLT中创建一个下拉元素,其中值和标签由冒号(:)和下一项的换行符分隔。
第一个我有一个普通的xml;(系统生成)
<root>
<item>
test1 : Test 1
test2 : Test 2
test3 : Test 3
</item>
</root>
然后拆分为:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="'
'"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<item><xsl:value-of select="normalize-space($text)"/></item>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</item>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
输出:<root>
<text>
<item>test1 : Test 1</item>
<item>test2 : Test 2</item>
<item>test3 : Test 3</item>
</text>
</root>
那我不知道接下来会发生什么?是否可以创建第二个模板来创建html选择?
<select>
<option value="test1">Test 1</option>
<option value="test2">Test 2</option>
<option value="test3">Test 3</option>
</select>
请helppppp…:"(
如果你把样式表改成:
XSLT 1.0<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="item">
<select>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</select>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<option value="{substring-before($token, ' : ')}">
<xsl:value-of select="substring-after($token, ' : ')"/>
</option>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
您将收到:
<html>
<body>
<select>
<option value="test1">Test 1</option>
<option value="test2">Test 2</option>
<option value="test3">Test 3</option>
</select>
</body>
</html>