问题描述
在
<xsl:template name="temp_name" mode="mode">
mode
是什么意思?我搜索了很多资源,但找不到示例.那么有人可以用一个例子来解释吗?
What is the meaning of mode
? I searched many many resource, but i couldn't find example for that. So can anybody explain with an example?
推荐答案
同时给模板一个名字和一个模式并没有太大意义.
name
属性完全标识一个模板,不能有两个同名不同模式的模板.
The name
attribute fully identifies a template and there cannot be two templates with the same name and different modes.
mode
属性允许使用不同的模式多次处理相同的节点.
The mode
attribute allows the same nodes to be processed more than once, using different modes.
这是一个简短的例子:
<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="num[position() mod 3 = 1]">
<tr>
<xsl:apply-templates mode="copy" select=
". | following-sibling::*[not(position() >2)]"/>
</tr>
</xsl:template>
<xsl:template match="*" mode="copy">
<td><xsl:value-of select="."/></td>
</xsl:template>
<xsl:template match="num"/>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
结果是数字显示在三个 tr
(行)中,每行包含三列(最后一行可能除外):
The result is that the numbers are displayed in three tr
(rows), each containing three columns (with the possible exception of the last row):
<tr>
<td>01</td>
<td>02</td>
<td>03</td>
</tr>
<tr>
<td>04</td>
<td>05</td>
<td>06</td>
</tr>
<tr>
<td>07</td>
<td>08</td>
<td>09</td>
</tr>
<tr>
<td>10</td>
</tr>
在这个转换中,任何 num
元素的位置不能以 3*k +1
的形式表示(其中 k
是一个整数),与空体模板匹配,因此不被处理.
In this transformation, any num
element with position that cannot be represented in the form 3*k +1
(where k
is an integer), is matched by a template with empty body and thus isn't processed.
然而,我们想要处理所有应该构成一行单元格的 num
元素.为此,我们使用 xslt 指令处理它们:
However, we want to process all num
elements that should form the cells of a row. For this purpuse, we are processing them using the xslt instruction:
<xsl:apply-templates mode="copy" select=
". | following-sibling::*[not(position() >2)]"/>
这意味着:不应用于通常会应用的所选节点模板(在无模式下),而是应用在copy
模式下的模板"
which means: "Do not apply to the selected nodes templates that would normally be applied (in no mode), but apply templates that are in copy
mode"
因此,我们不会忽略所选的 num
元素,而是以 copy
模式 处理它们并创建 td
s 一行.
Thus, we do not ignore the selected num
elements, but are processing them in copy
mode and are creating the td
s of a row.
模板规则:
<xsl:template match="num"/>
必须覆盖 xslt 内置模板(默认处理),否则会导致 num
节点的字符串值的位置不能表示为 3*k +1
,要输出.
is necessary to override the xslt builtin templates (default processing) that would otherwise cause the string values of the num
nodes whose position cannot be represented as 3*k +1
, to be output.
因此,这些节点由两个模板处理:
<xsl:template match="num"/>
和
<xsl:apply-templates mode="copy" select=
". | following-sibling::*[not(position() >2)]"/>
因此我们得到了想要的结果.
and thus we get the wanted result.
使用一个好的 XSLT 调试器来逐步了解这些模板是如何应用的会很有启发性.
这篇关于能给我举个“模式"的例子吗?xsl 中的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!