2013-10-22 117 views
0

我在我的XSLT中有一个for-each语句来检查是否有多个产品图像。如果多于一个然后显示图像。我需要另一个条件需要显示4张图片,可以在我的for-each语句中包含这个。现在我的for-each语句就像。for-each in XSLT for image

<xsl:for-each select="$extraimages/productimages/productimage[position() &gt; 1 and extension != '.pdf']"> 
    <li> 
     Something to do 
    </li> 
</xsl:for-each> 

回答

1

因此,您需要检查是否有多个图像。当有零或一个时,什么都不显示,当有多个时,显示(最多)前四个?那么怎么样

<xsl:if test="count($extraimages/productimages/productimage) &gt; 1"> 
    <xsl:for-each select="($extraimages/productimages/productimage)[position() &lt;= 4]"> 
    <li>something</li> 
    </xsl:for-each> 
</xsl:if> 

括号有所作为,如果有一个以上的productimages元素$extraimages - 用括号你会得到不超过四个图像,没有他们,你会得到所有productimage元素是在其各自的productimages子元素的前四个productimage子元素内,其可能总共超过四个。

您还可以在你的榜样的extension检查中的问题,纳入你会做这样的事情

<xsl:if test="count($extraimages/productimages/productimage[extension != '.pdf']) &gt; 1"> 
    <xsl:for-each select="($extraimages/productimages/productimage[extension != '.pdf'])[position() &lt;= 4]"> 
    <li>something</li> 
    </xsl:for-each> 
</xsl:if> 

再次括号可能会或可能不会取决于$extraimages结构是必要的。

如果你想显示的图像2-5而不是1-4,那么你不需要if,它只是变得

<xsl:for-each select=" 
     ($extraimages/productimages/productimage[extension != '.pdf']) 
     [position() &gt; 1][position() &lt;= 5]"> 
    <li>something</li> 
</xsl:for-each> 

因为select将选择什么都没有,如果有比少两个非pdf图像。

2

如果我明白正确:

<xsl:for-each select="$extraimages/productimages/productimage[position() &gt; 1 and position() &lt;= 5 and extension != '.pdf']"> 
    <li> 
     Something to do 
    </li> 
</xsl:for-each>