能给我一个“模式”的例子吗? xsl 中的模板?

发布于 2024-10-08 13:38:44 字数 148 浏览 0 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-10-15 13:38:44

给模板同时指定名称和模式并没有多大意义

name 属性完全标识了一个模板,并且不能有两个具有相同名称和不同模式的模板。

mode 属性允许使用不同的模式多次处理相同的节点

下面是一个简短的示例:

<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(行)中,每行包含三列(最后一行可能例外):

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

在此转换中,任何位置无法以 3*k +1< 形式表示的 num 元素/code> (其中 k 是一个整数),与空主体的模板匹配,因此不会被处理。

但是,我们想要处理应形成行单元格的所有 num 元素。为此,我们使用 xslt 指令处理它们:

<xsl:apply-templates mode="copy" select=
 ". | following-sibling::*[not(position() >2)]"/>

这意味着:“不要应用于通常应用(在无模式下)的选定节点模板,而是应用 copy 中的模板 因此

,我们不会忽略所选的num元素,而是以复制模式处理它们并创建td< /code> 一行。

模板规则:

<xsl:template match="num"/>

需要覆盖xslt内置模板(默认处理),否则会导致num个节点的位置字符串值无法表示为3*k +1,要输出。

因此,这些节点由两个模板处理

<xsl:template match="num"/>

因此

<xsl:apply-templates mode="copy" select=
 ". | following-sibling::*[not(position() >2)]"/>

我们得到了想要的结果。

使用良好的 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:

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

When this transformation is applied on the following XML document:

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

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>

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.

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)]"/>

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 in copy mode and are creating the td s of a row.

The template rule:

<xsl:template match="num"/>

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.

So, these nodes are processed by both templates:

<xsl:template match="num"/>

and

<xsl:apply-templates mode="copy" select=
 ". | following-sibling::*[not(position() >2)]"/>

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.

独行侠 2024-10-15 13:38:44

mode 属性允许以多种方式处理相同的 XML 元素。

如果想要使用 mode 属性,模板必须具有 match 属性,因此它们不适用于仅依赖 name 属性的模板打电话。

它们应用于 xsl:apply-templates 来确定哪个 xsl:template 将响应,因此使用模式的应用只会调用使用相同模式的模板。没有模式的模板将仅响应没有模式的应用。

例如,您可能希望将 XML paragraph 元素呈现为 HTML p 元素以进行查看,但呈现为 form 进行编辑。

然后可以通过以下方式呈现以供查看:

<xsl:template match="paragraph">
 <p>...</p>
</xsl:template>

调用 through 时,它将与 XML 文档中的 paragraph 标签匹配:

<xsl:apply-templates />

相反,呈现以供编辑使用:

<xsl:template match="paragraph" mode="edit">
 <form>...</form>
</xsl:template>

通过调用 through:

<xsl:apply-templates mode="edit" />

请注意,在示例中,如果其余部分要使用非模式 xsl:apply-templates 呈现页面,则必须显式调用 xsl:template mode="edit" 版本要编辑的特定 paragraph 元素,例如:

<xsl:apply-templates select="paragraph[@id=$id_to_edit]" mode="edit" />

模式允许将同一元素树呈现为完全不同的结构,例如在编辑元素时,但其子元素必须呈现在紧凑的摘要中格式,使用与父级相同的模式名称以保持一致性,而不是正常的完整渲染。这些紧凑格式可以有一个按钮来重新渲染页面,并将该子元素作为正在编辑的元素。

The mode attribute allows multiple ways of processing the same XML elements.

A template must have a match attribute if wanting to use a mode attribute, so they are not meant for templates relying solely upon the name attribute for calling.

They apply to xsl:apply-templates to determine which xsl:templates 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 HTML p element for viewing, but as a form for editing.

This can then be rendered for viewing by:

<xsl:template match="paragraph">
 <p>...</p>
</xsl:template>

which would match paragraph tags in an XML document when called through:

<xsl:apply-templates />

Conversely, to render for editing use:

<xsl:template match="paragraph" mode="edit">
 <form>...</form>
</xsl:template>

by calling through:

<xsl:apply-templates mode="edit" />

Note that in the example, if the rest of the page is to be rendered using the non-mode xsl:apply-templates, the xsl:template mode="edit" version would have to be explicitly invoked for the specific paragraph element to be edited, like:

<xsl:apply-templates select="paragraph[@id=$id_to_edit]" mode="edit" />

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.

你爱我像她 2024-10-15 13:38:44
<xsl:apply-templates select="phone" />
<xsl:apply-templates select="phone" mode="accountNumber"/>
<xsl:template match="phone">
      <TD>A</TD>
</xsl:template>
<xsl:template match="phone" mode="accountNumber">
      <TD>B</TD>
</xsl:template>

在此处链接一个简单的示例: https:// msdn.microsoft.com/en-us/library/ms256045%28v=vs.110%29.aspx

<xsl:apply-templates select="phone" />
<xsl:apply-templates select="phone" mode="accountNumber"/>
<xsl:template match="phone">
      <TD>A</TD>
</xsl:template>
<xsl:template match="phone" mode="accountNumber">
      <TD>B</TD>
</xsl:template>

Link a simple example here: https://msdn.microsoft.com/en-us/library/ms256045%28v=vs.110%29.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文