XSLT for-each循环和格式化表行

时间:2011-03-04 19:58:07

标签: xml xslt formatting

我如何修改下面的xsl,以便在放置图像的for-each循环中每个表行放置2个图像,而不是4个(它当前的作用)。

我认为它与mod功能有关?

我尝试将foreach兄弟循环拆分到另一行,但只有第一个图像显示在第一行,其余图像显示在第二行。

<xsl:for-each select="Finding">
  <xsl:if test="FindingType = 'A'">
  <xsl:variable name="nImage" select="count(Image)" />
    <tr><td class="di"><table width="100%" class="findings">
  <tr><td class="shade" colspan="4"><p class="g"><b>Finding</b></p></td></tr>
  <xsl:for-each select="Image[position() mod 4 = 1]">
<tr><td width="25%" align="left" valign="top"><p class="g"><img src="{.}" height="128" width="128" hspace="12"/><br />
    <xsl:value-of select="@Type" /><xsl:text> </xsl:text>    <xsl:value-of select="@SubType" />
    <xsl:if test="@Position != ''"> (<xsl:value-of select="@Position" />)         </xsl:if><br /><br />
</p>
    </td></tr>
<tr><xsl:for-each select=". | following-sibling::Image[position() &lt; 4 and (position() mod 4 = 0 or position() mod 4 = 1 or position() mod 4 = 2)]">
    <td width="25%" valign="top" align="left"><p class="g">
    <xsl:if test="(string-length(following-sibling::Image/.) > 0)">
    <img src="{following-sibling::Image/.}" height="128" width="128" hspace="12" />
    </xsl:if>
    <br />
        <xsl:value-of select="following-sibling::Image/@Type" />        <xsl:text> </xsl:text>    
    <xsl:value-of select="following-sibling::Image/@SubType" />
        <xsl:if test="following-sibling::Image/@Position != ''"> (<xsl:value-of select="following-sibling::Image/@Position" />)         </xsl:if>
<br /><br /></p>
</td>
</xsl:for-each>
  <xsl:if test="$nImage = 2">
    <td width="25%" />
  </xsl:if>
</tr>

2 个答案:

答案 0 :(得分:7)

答案很简单:将4替换为2,或者甚至更好地定义值为pNumCols的全局参数2

您将进行以下更改:

  1. 在全球范围内添加此行<xsl:param name="pNumCols" select="2"/>

  2. 替换

    <xsl:for-each select="Image[position() mod 4 = 1]">

  3. <xsl:for-each select="Image[position() mod $pNumCols = 1]">

    0.3。的替换

    <xsl:for-each select=". | following-sibling::Image
                  [position() &lt; 4 
                 and (position() mod 4 = 0 or position() mod 4 = 1 
                     or position() mod 4 = 2)]">
    

    使用:

    <xsl:for-each select=". | following-sibling::Image
                                   [not(position() >= $pNumCols)]">
    

    请注意

    <xsl:for-each select=". | following-sibling::Image
                  [position() &lt; 4 
                 and (position() mod 4 = 0 or position() mod 4 = 1 
                     or position() mod 4 = 2)]">
    

    等同于较短的:

    <xsl:for-each select=". | following-sibling::Image[position() &lt; 4 ]">
    

答案 1 :(得分:1)

是的,您应该将Image[position() mod 4 = 1]更改为使用2而不是4。 在. | following-sibling::Image[position() < 4 and (position() mod 4 = 0 or position() mod 4 = 1 or position() mod 4 = 2)]下面变为. | following-sibling::Image[position() < 2 and (position() mod 2 = 0)]

这应该足够了。