能给我一个“模式”的例子吗? xsl 中的模板?
在
<xsl:template name="temp_name" mode="mode">
模式
的含义是什么?我搜索了很多资源,但找不到相关示例。那么有人可以用例子解释一下吗?
In
<xsl:template name="temp_name" 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给模板同时指定名称和模式并没有多大意义。
name
属性完全标识了一个模板,并且不能有两个具有相同名称和不同模式的模板。mode
属性允许使用不同的模式多次处理相同的节点。下面是一个简短的示例:
当此转换应用于以下 XML 文档时:
结果是数字显示在三个
tr
(行)中,每行包含三列(最后一行可能例外):在此转换中,任何位置无法以
3*k +1< 形式表示的
num
元素/code> (其中k
是一个整数),与空主体的模板匹配,因此不会被处理。但是,我们想要处理应形成行单元格的所有
num
元素。为此,我们使用 xslt 指令处理它们:这意味着:“不要应用于通常应用(在无模式下)的选定节点模板,而是应用
copy
中的模板 因此,我们不会忽略所选的
num
元素,而是以复制
模式处理它们并创建td< /code> 一行。
模板规则:
需要覆盖xslt内置模板(默认处理),否则会导致
num
个节点的位置字符串值无法表示为3*k +1,要输出。
因此,这些节点由两个模板处理:
因此
我们得到了想要的结果。
使用良好的 XSLT 调试器来逐步了解如何应用这些模板会很有帮助。
It isn't too meaningful to give a template both a name and a mode.
The
name
attribute fully identifies a template and there cannot be two templates with the same name and different modes.The
mode
attribute allows the same nodes to be processed more than once, using different modes.Here is a short example:
When this transformation is applied on the following XML document:
The result is that the numbers are displayed in three
tr
(rows), each containing three columns (with the possible exception of the last row):In this transformation, any
num
element with position that cannot be represented in the form3*k +1
(wherek
is an integer), is matched by a template with empty body and thus isn't processed.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: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"Thus, we do not ignore the selected
num
elements, but are processing them incopy
mode and are creating thetd
s of a row.The template rule:
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 as3*k +1
, to be output.So, these nodes are processed by both templates:
and
and thus we get the wanted result.
It would be instructive to step through with a good XSLT debugger in order to see how these templates are applied.
mode
属性允许以多种方式处理相同的 XML 元素。如果想要使用
mode
属性,模板必须具有match
属性,因此它们不适用于仅依赖name
属性的模板打电话。它们应用于 xsl:apply-templates 来确定哪个 xsl:template 将响应,因此使用模式的应用只会调用使用相同模式的模板。没有模式的模板将仅响应没有模式的应用。
例如,您可能希望将 XML
paragraph
元素呈现为 HTMLp
元素以进行查看,但呈现为form
进行编辑。然后可以通过以下方式呈现以供查看:
调用 through 时,它将与 XML 文档中的
paragraph
标签匹配:相反,呈现以供编辑使用:
通过调用 through:
请注意,在示例中,如果其余部分要使用非模式
xsl:apply-templates
呈现页面,则必须显式调用xsl:template mode="edit"
版本要编辑的特定paragraph
元素,例如:模式允许将同一元素树呈现为完全不同的结构,例如在编辑元素时,但其子元素必须呈现在紧凑的摘要中格式,使用与父级相同的模式名称以保持一致性,而不是正常的完整渲染。这些紧凑格式可以有一个按钮来重新渲染页面,并将该子元素作为正在编辑的元素。
The
mode
attribute allows multiple ways of processing the same XML elements.A template must have a
match
attribute if wanting to use amode
attribute, so they are not meant for templates relying solely upon thename
attribute for calling.They apply to
xsl:apply-templates
to determine whichxsl:template
s will respond, so an apply using a mode will only invoke a template that uses the same mode. Templates with no mode will only respond to an apply without a mode.For example, you may want to render an XML
paragraph
element as a HTMLp
element for viewing, but as aform
for editing.This can then be rendered for viewing by:
which would match
paragraph
tags in an XML document when called through:Conversely, to render for editing use:
by calling through:
Note that in the example, if the rest of the page is to be rendered using the non-mode
xsl:apply-templates
, thexsl:template mode="edit"
version would have to be explicitly invoked for the specificparagraph
element to be edited, like:Modes allow for rendering of the same tree of elements into a completely different structure, such as when editing an element, but its children have to be rendered in a compact summary format, using the same mode name as the parent for consistency, rather than their normal full rendering. Those compact formats could have a button to re-render the page with that child as the element being edited.
在此处链接一个简单的示例: https:// msdn.microsoft.com/en-us/library/ms256045%28v=vs.110%29.aspx
Link a simple example here: https://msdn.microsoft.com/en-us/library/ms256045%28v=vs.110%29.aspx