xslt - 添加 </tr><tr>每 n 个节点?
我在这里找到了解决该问题的优雅解决方案:xsl for-each:每 n 行添加代码块?
I found an elegant solution for that problem here: xsl for-each: add code block every n rows?
我想了解 xslt 代码,我想知道您是否可以通过查看上面提供的链接来帮助我理解它.
I'd like to understand the xslt code and I was wondering if you could help me to understand it by taking a look at the link provided above.
基本上有 3 个 .对我来说,前两个足以达到目的.但是我只尝试了 2
<xsl:template>
并且它不起作用.简而言之,第三个是必需的.这是:
Basically there are 3 <xsl:template>
. To me the first 2 ones are enough to achieve the purpose. However I tried with only 2 <xsl:template>
and it doesn't work. In short the third one is required. Here it is:
<xsl:template match="gallery[not(position() mod 6 = 1)]"/>
第二个模板有模式,而最后一个没有.
The second template has a mode while the last one has not.
我不知道最后一个什么时候执行.你能帮我弄清楚吗?
I have no idea when the last one is executed. Could you please help me to figure out it?
感谢您的帮助.
问候,
罗兰
这是您要询问的完整代码.我碰巧是作者,所以让我解释一下:
Here is the complete code you were asking about. I happen to be the author, so let me explain:
<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="*"/>
以下模板覆盖了元素节点的 XSLT 内置模板.它匹配每 6k+1 个 gallery
元素.它创建了一个 tr
元素,并在它的主体内部将处理这个 gallery
元素的结果与接下来的 5 个元素放在一起.处理是在特殊模式(proc")下完成的以将其与 XSLT 内置模板启动并继续运行的默认匿名模式区分开来.
The following template overrides the XSLT built-in template for element nodes.
It matches every 6k+1th gallery
element. It cretes a tr
element and inside its body puts the results of processing this gallery
element togeether with the next 5. The processing is done in a special mode ("proc") to distinguish this from the default anonymous mode in which the XSLT built-in templates started and continue to operate.
<xsl:template match="gallery[position() mod 6 = 1]">
<tr>
<xsl:apply-templates mode="proc"
select=".|following-sibling::gallery[not(position() > 5)]"
/>
</tr>
</xsl:template>
以下模板在proc"模式下调用,以处理应在同一行中的 6 个组中的每个 gallery
元素.
The following template is invoked in mode "proc" to process every gallery
element in a group of 6 that should be in the same row.
<xsl:template match="gallery" mode="proc">
<td>
<img src="{gallery-image-location}" alt="{gallery-image-alt}"/>
</td>
</xsl:template>
以下模板覆盖了所有 gallery
元素的 XSLT 内置模板的默认处理,这些元素的位置不是 6k+1 类型(它们不是开始新的 6 元组).它只是说不要对任何这样的元素做任何事情,因为这些元素已经在proc"模式下处理过.
The following template overrides the default processing of the XSLT built-in templates for all gallery
elements, whose position is not of the type 6k+1 (they are not starting a new 6-tuple). It says simply not to do anything with any such element, because these elements are already processed in "proc" mode.
<xsl:template match="gallery[not(position() mod 6 = 1)]"/>
</xsl:stylesheet>
您需要熟悉 XSLT 的处理模型、默认处理和内置模板.
You need to acquaint yourself with XSLT's processing model, default processing and built-in templates.