gpt4 book ai didi

html - xsl for-each : add code block every n rows?

转载 作者:搜寻专家 更新时间:2023-10-31 02:25:05 25 4
gpt4 key购买 nike

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

我有这个:

<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>';
}
}

最佳答案

How do I add the row break every six or so columns with 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 - xsl for-each : add code block every n rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3806578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com