【发布时间】:2015-03-20 06:00:00
【问题描述】:
我有一个元素列表:
<vehiciles>
<vehicile value="_CAR">CAR</vehicile>
<vehicile value="01">vehicile1</vehicile>
<vehicile value="02">vehicile2</vehicile>
<vehicile value="03">vehicile3</vehicile>
<vehicile value="_TRUCK">TRUCK</vehicile>
<vehicile value="04">vehicile4</vehicile>
<vehicile value="05">vehicile5</vehicile>
<vehicile value="06">vehicile6</vehicile>
</vehiciles>
不幸的是,我无法更改结构,但我必须按车辆指示的类别(在 html select/optgroup 标记中)分组,该值以下划线开头。
我想达到的结果:
<select>
<optgroup label="CAR">
<option value="01">vehicile1</option>
<option value="02">vehicile2</option>
<option value="03">vehicile3</option>
</optgroup>
<opgroup label="TRUCK">
<option value="04">vehicile4</option>
<option value="05">vehicile5</option>
<option value="06">vehicile6</option>
</optgroup>
</select>
我尝试的是:
<xsl:template match="field" mode="dropdown_list">
<select>
<xsl:choose>
<xsl:when test="vehiciles/vehicile[starts-with(@value, '_')]">
<xsl:for-each select="vehiciles/vehicile[starts-with(@value, '_')]">
<xsl:variable name="lastValue" select="following-sibling::*[starts-with(@value, '_')][@value]" />
<optgroup>
<xsl:attribute name="label">
<xsl:value-of select="text()"/>
</xsl:attribute>
<xsl:for-each select="following-sibling::*[not(preceding::vehicile[1][@value = $lastValue])]">
<option value="{@value}">
<xsl:value-of select="text()"/>
</option>
</xsl:for-each>
</optgroup>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<!-- something here -->
</xsl:otherwise>
</xsl:choose>
</select>
</xsl:template>
它输出第二个循环不错,但首先包含所有元素。尝试了几个小时没有运气。
尝试通过递归进行但失败以及 Muenchian 分组。
有没有办法从某个节点向上查找符合条件的第一个兄弟节点?还是其他方式?
任何帮助表示赞赏。
【问题讨论】:
标签: xml xslt xpath xslt-1.0 xslt-grouping