【问题标题】:xsl for-each: add code block every n rows?xsl for-each:每n行添加代码块?
【发布时间】:2025-11-26 02:05:02
【问题描述】:

我正在尝试将代表图片库的一些 xml 转换为 html 表。 (必须使用 html 而不是 css 来完成)。 如何使用 xsl 每六列左右添加换行符</tr><tr>

我有这个:

<xsl:for-each select="//email/gallery" >
<td>
    <img>
    <xsl:attribute name="src">
        <xsl:value-of select="gallery-image-location"/>
    </xsl:attribute>
    <xsl:attribute name="alt">
        <xsl:value-of select="gallery-image-alt"/>
    </xsl:attribute>
    </img>
</td>
<xsl:if test="????">
    </tr>
    <tr>
</xsl:if>
<xsl:for-each>

在 Javascript 中,我会执行以下操作:

for (i=0; i<gallery.length; i++) {
    htm += '<td><img src="' +
    gallery[i].gallery-image-location +
    '" alt="'+ gallery[i].gallery-image-alt +'"></td>';

    if (i%6 == 5 && i != gallery.length-1) {
        htm += '</tr><tr>';
    }
}

【问题讨论】:

  • 这是一个常见问题解答,但可以 +1 提问。请参阅我的答案以获得正确且简短的 XSLT 解决方案。

标签: html xslt transform foreach


【解决方案1】:

如何添加换行符 xsl 每六列左右?

在 XSLT 中你不需要!

XSLT 处理节点,而不是标签。

这是 XSLT 的位置分组方式

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="gallery[position() mod 6 = 1]">
  <tr>
   <xsl:apply-templates mode="proc"
        select=".|following-sibling::gallery[not(position() > 5)]"
   />
  </tr>
 </xsl:template>

 <xsl:template match="gallery" mode="proc">
  <td>
    <img src="{gallery-image-location}" alt="{gallery-image-alt}"/>
  </td>
 </xsl:template>

 <xsl:template match="gallery[not(position() mod 6 = 1)]"/>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时

<email>
    <gallery>
        <gallery-image-location>http://server/picts/1</gallery-image-location>
        <gallery-image-alt>Description 1</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/2</gallery-image-location>
        <gallery-image-alt>Description 2</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/3</gallery-image-location>
        <gallery-image-alt>Description 3</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/41</gallery-image-location>
        <gallery-image-alt>Description 4</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/5</gallery-image-location>
        <gallery-image-alt>Description 5</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/6</gallery-image-location>
        <gallery-image-alt>Description 6</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/7</gallery-image-location>
        <gallery-image-alt>Description 7</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/8</gallery-image-location>
        <gallery-image-alt>Description 8</gallery-image-alt>
    </gallery>
    <gallery>
        <gallery-image-location>http://server/picts/9</gallery-image-location>
        <gallery-image-alt>Description 9</gallery-image-alt>
    </gallery>
</email>

产生想要的正确结果

<tr>
    <td>
        <img src="http://server/picts/1" alt="Description 1"/>
    </td>
    <td>
        <img src="http://server/picts/2" alt="Description 2"/>
    </td>
    <td>
        <img src="http://server/picts/3" alt="Description 3"/>
    </td>
    <td>
        <img src="http://server/picts/41" alt="Description 4"/>
    </td>
    <td>
        <img src="http://server/picts/5" alt="Description 5"/>
    </td>
    <td>
        <img src="http://server/picts/6" alt="Description 6"/>
    </td>
</tr>
<tr>
    <td>
        <img src="http://server/picts/7" alt="Description 7"/>
    </td>
    <td>
        <img src="http://server/picts/8" alt="Description 8"/>
    </td>
    <td>
        <img src="http://server/picts/9" alt="Description 9"/>
    </td>
</tr>

【讨论】:

  • 嗨。我正在使用相同的解决方案并且效果很好,谢谢。但是为了让生成的 HTML 包含 html、body 和 table 标记,我必须进行哪些更改?
  • @Maxxer,只需将这些元素添加到与/ 匹配的模板中,然后将模板应用到&lt;body&gt; 的正文中
【解决方案2】:

如果您使用的是 XSLT 2

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="email">
   <xsl:for-each-group select="gallery" group-by="(position() - 1) idiv 6">
     <tr>
       <xsl:apply-templates select="current-group()"/>
     </tr>
   </xsl:for-each-group>
 </xsl:template>

 <xsl:template match="gallery">
  <td>
    <img src="{gallery-image-location}" alt="{gallery-image-alt}"/>
  </td>
 </xsl:template>

</xsl:stylesheet>

【讨论】:

  • 一个小问题:group-by="position() idiv 6" 实际上必须是:group-by="(position() -1) idiv 6
  • @Nick-Jones:难道你没有看到除非你应用推荐的修复,否则你提出的解决方案是不正确的吗?
  • @Dimitre Novatchev:对不起,已经离开了,现在修好了。
【解决方案3】:

首先,假设您使用的是 XML 或 HTML 的输出格式,我认为您不能像使用 &lt;/tr&gt;&lt;tr&gt; 段一样放置不匹配的标签。 XSL(在这些模式下)不仅仅像使用 Javascript 那样产生字符串输出。 (不过我可能错了。)

你在那里所做的事情与分页密切相关;你可以看看分页脚本。

这是我的一个(未经测试的)建议:

<!-- For every sixth item, starting with the first... -->
<xsl:for-each select="//email/gallery[position() mod 6 = 1]">
  <tr>
     <!-- Get that item's position... -->
     <xsl:variable name="thisPos" select="position()" />

     <!-- and select the six (or less) items starting with that position. -->
     <xsl:for-each select="//email/gallery[position() &gt;= $thisPos and position() &lt; $thisPos + 6]">
       <td><img>
        <xsl:attribute name="src">
          <xsl:value-of select="gallery-image-location"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
          <xsl:value-of select="gallery-image-alt"/>
        </xsl:attribute>
       </img></td>
     </xsl:for-each>
  </tr>
</xsl:for-each>

哦,还有 IIRC,循环的内部也可以缩短一点:

<td><img src="{gallery-image-location}" alt="{gallery-image-alt}" /></td>

那些花括号将帮助您在长脚本上保持理智。

【讨论】:

  • 为什么不迭代 self 和 5 个下一个后续兄弟姐妹?
  • 呃,没想到。我猜,我的内心仍然是一个 C 程序员。
  • 这个例子并没有真正起作用,而且绝对不是正确的答案,但与我们最终使用的很接近(这里也是 C 程序员的核心;)。 [position() >= $thisPos 和 position() < $thisPos + 6] 没有给出想要的结果。我最终使用了 [position() > $thisPos*6-6 和 position() < $thisPos*6+1]
  • 哦,呃...我试图使用...那个,呃...就像我说的,C程序员。