使用XSLT 1.0有条件地对节点进行分组

时间:2017-03-24 11:43:09

标签: xslt xslt-1.0

我正在使用以下内容迭代节点组:

    <xsl:for-each select="NewDataSet/VehicleDetail/Options/Option">
    <xsl:choose>
        <xsl:when test="string-length(.) > 40">
            <div class="large">
                <xsl:value-of select="."/>
            </div>
        </xsl:when>
        <xsl:otherwise>
            <div class="small">
                <xsl:value-of select="."/>
            </div>
        </xsl:otherwise>
    </xsl:choose>
</xsl:for-each>

我希望能够做的是将大件物品(40个字符+)和小件物品(40个字符或更少)分组如下:

<div class="largeItems">
  <div class="large">Large Item</div>
  <div class="large">Large Item</div>
  <div class="large">Large Item</div>
  <div class="large">Large Item</div>
  <div class="large">Large Item</div>
</div>
<div class="smallItems">
  <div class="small">Small Item</div>
  <div class="small">Small Item</div>
  <div class="small">Small Item</div>
  <div class="small">Small Item</div>
  <div class="small">Small Item</div>
</div>

感谢。

1 个答案:

答案 0 :(得分:1)

尝试:

<div class="largeItems">
    <xsl:for-each select="NewDataSet/VehicleDetail/Options/Option[string-length() > 40]">
        <div class="large">
            <xsl:value-of select="."/>
        </div>
    </xsl:for-each>
</div>
<div class="smallItems">
    <xsl:for-each select="NewDataSet/VehicleDetail/Options/Option[string-length() &lt;= 40]">
        <div class="small">
            <xsl:value-of select="."/>
        </div>
    </xsl:for-each>
</div>

或者,如果您更喜欢重复代码:

<div class="largeItems">
    <xsl:apply-templates select="NewDataSet/VehicleDetail/Options/Option[string-length() > 40]">
        <xsl:with-param name="class" select="'large'"/>
    </xsl:apply-templates>
</div>
<div class="smallItems">    
    <xsl:apply-templates select="NewDataSet/VehicleDetail/Options/Option[string-length() > 40]">
        <xsl:with-param name="class" select="'small'"/>
    </xsl:apply-templates>
</div>  

然后:

<xsl:template match="Option">
    <xsl:param name="class"/>
    <div class="{$class}">
        <xsl:value-of select="."/>
    </div>
</xsl:template>

未经测试,因为既未提供输入也未提供上下文。