我尝试使用XSLT创建热文本交互测验的下拉列表,但是为每个元素生成并显示它。在这里,我需要将所有值显示在下拉列表中。我该怎么做XSLT?
XML:
<hottextInteraction responseIdentifier="RESPONSE_1" maxChoices="0">
<prompt>Hot text multi selection</prompt>
<div>
<p>The
<hottext identifier="X99999-t01as02asi006asic001">apple</hottext>
<hottext identifier="X99999-t01as02asi006asic002">pear</hottext>
<hottext identifier="X99999-t01as02asi006asic003">carrot</hottext>
<hottext identifier="X99999-t01as02asi006asic004">potatoe</hottext> grow on trees.</p>
<p>The
<hottext identifier="X99999-t01as02asi006asic005">cherry</hottext>
<hottext identifier="X99999-t01as02asi006asic006">watermelon</hottext>
<hottext identifier="X99999-t01as02asi006asic007">orange</hottext>
<hottext identifier="X99999-t01as02asi006asic008">potatoe</hottext> grow in soil.</p>
</div>
</hottextInteraction>
XSLT:
<xsl:template match="//hottextInteraction"><div class="quizborder"><xsl:apply-templates /></div></xsl:template>
<xsl:template match="//hottext"><select name="hottext"><option value="hottext"><xsl:value-of select="." /></option></select></xsl:template>
输出:
<div>
<div class="quizborder">
<p>Hot text multi selection</p>
<div>
The
<select name="hottext"><option value="hottext">apple</option></select>
<select name="hottext"><option value="hottext">pear</option></select>
<select name="hottext"><option value="hottext">carrot</option></select>
<select name="hottext"><option value="hottext">potatoe</option></select> grow on trees.
The
<select name="hottext"><option value="hottext">cherry</option></select>
<select name="hottext"><option value="hottext">watermelon</option></select>
<select name="hottext"><option value="hottext">orange</option></select>
<select name="hottext"><option value="hottext">potatoe</option></select> grow in soil.
</div>
</div>
</div>
我需要结果:
<div>
The
<select name="hottext">
<option value="apple">apple</option>
<option value="pear">pear</option>
<option value="carrot">carrot</option>
<option value="potatoe">potatoe</option>
</select>
grow on trees.
The
<select name="hottext">
<option value="cherry">cherry</option>
<option value="watermelon">watermelon</option>
<option value="orange">orange</option>
<option value="potatoe">potatoe</option>
</select> grow in soil.
</div>
答案 0 :(得分:1)
一种方法是使用mode
轴上的following-siblings
属性来创建option
元素:
<xsl:template match="hottext[position() = 1]"> <!-- wrap following-siblings in 'select' element -->
<select name="hottext">
<xsl:apply-templates select=". | following-sibling::hottext" mode="next"/>
</select>
</xsl:template>
<xsl:template match="hottext" mode="next"> <!-- create option elements -->
<option value="hottext">
<xsl:value-of select="." />
</option>
</xsl:template>
<xsl:template match="hottext" /> <!-- skips hottext elements without 'mode' -->
要摆脱多余的空白区域,您可以使用
<xsl:strip-space elements="hottext" />
在样式表的开头。
答案 1 :(得分:0)
如果您对查看XSLT 2.0解决方案感兴趣,可以使用xsl:for-each-group
对相邻的hottext
个节点进行分组。试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:strip-space elements="*" />
<xsl:template match="hottextInteraction">
<div class="quizborder">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="hottext">
<option value="{.}">
<xsl:value-of select="." />
</option>
</xsl:template>
<xsl:template match="*[hottext]">
<xsl:for-each-group select="node()" group-adjacent="boolean(self::hottext)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<select>
<xsl:apply-templates select="current-group()" />
</select>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
注意使用<xsl:strip-space elements="*" />
来忽略hottext
个节点之间的空白节点。