简体   繁体   English

xsl for-each:每n行添加代码块?

[英]xsl for-each: add code block every n rows?

I am trying to transform a bit of xml which represents an image gallery into an html table. 我试图将一些表示图像库的xml转换为html表。 (it must be done with html and not with css). (必须使用html而不是css)。 How do I add the row break </tr><tr> every six or so columns with xsl? 如何使用xsl每隔六个左右添加行中断</tr><tr>

I have this: 我有这个:

<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>

In Javascript I would do something like: 在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? 如何使用xsl每隔六个左右添加行中断?

In XSLT you don't! 在XSLT中你没有!

XSLT processes nodes , not tags. XSLT处理节点 ,而不是标签。

Here is the XSLT way of positional grouping : 这是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>

when this transformation is applied on the following XML document : 当此转换应用于以下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>

the wanted, correct result is produced : 产生了想要的正确结果

<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>

If you are using XSLT 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>

First, assuming that you are using an output format of XML or HTML, I don't think you can place unmatched tags as your are with the </tr><tr> segment. 首先,假设您使用的是XML或HTML的输出格式,我认为您不能像使用</tr><tr>段那样放置不匹配的标签。 XSL (in these modes) doesn't just produce string output the way you would with your Javascript. XSL(在这些模式下)不仅像你的Javascript一样产生字符串输出。 (I could be wrong about this though.) (虽然我可能错了。)

What you're doing there is closely related to paging; 你在那里做的与寻呼密切相关; you might look at paging scripts. 你可能会看一下分页脚本。

Here's an (untested) suggestion from me: 这是我的一个(未经测试的)建议:

<!-- 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>

Oh, and IIRC, the interior of the loop can be shortened a little bit too: 哦,和IIRC,循环的内部也可以缩短一点:

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

Those curly braces will help save your sanity on long scripts. 那些花括号将有助于在长脚本上保存你的理智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM