XSL:“xsl:template”的“match=”/“”的含义

发布于 2024-09-07 00:57:29 字数 202 浏览 4 评论 0原文

我正在学习 XML 以及如何使用 XSL 文件。 在 XSL 文件中,我发现了以下术语:

xsl:template match="/"

这代表什么? 我可以用什么来代替 / ? 我可以编写 table 或任何其他 HTML 标记来代替 / 吗?

I am just learning XML and how to use XSL files.
In an XSL file I found the following term:

xsl:template match="/"

What does this stand for?
And what could I use instead of the /?
Could I write table or any other HTML tag instead of /?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

请恋爱 2024-09-14 00:57:29

指令的 match 属性值必须是匹配模式。

匹配模式构成所有可能的 XPath 表达式集合的子集。第一个自然的限制是匹配模式必须选择一组节点。还有其他限制。特别是,位置步骤中不允许使用反向轴(但可以在谓词中指定)。此外,XSLT 1.0 中不允许使用变量或参数引用,但在 XSLT 2.x 中使用它们是合法的。

XPath 中的 / 表示或文档节点。在 XPath 2.0(以及 XSLT 2.x)中,这也可以写为 document-node()

匹配模式可以包含 // 缩写

匹配模式示例:

<xsl:template match="table">

可以应用于任何名为table的元素。

<xsl:template match="x/y">

可以应用于任何名为 y 的元素,其父元素是名为 x 的元素。

<xsl:template match="*">

可以应用于任何元素。

<xsl:template match="/*">

只能应用于 XML 文档的顶部元素。

<xsl:template match="@*">

可以应用于任何属性。

<xsl:template match="text()">

可以应用于任何文本节点。

<xsl:template match="comment()">

可以应用于任何评论节点。

<xsl:template match="processing-instruction()">

可以应用于任何处理指令节点。

<xsl:template match="node()">

可以应用于任何节点:元素、文本、注释或处理指令。

The value of the match attribute of the <xsl:template> instruction must be a match pattern.

Match patterns form a subset of the set of all possible XPath expressions. The first, natural, limitation is that a match pattern must select a set of nodes. There are also other limitations. In particular, reverse axes are not allowed in the location steps (but can be specified within the predicates). Also, no variable or parameter references are allowed in XSLT 1.0, but using these is legal in XSLT 2.x.

/ in XPath denotes the root or document node. In XPath 2.0 (and hence XSLT 2.x) this can also be written as document-node().

A match pattern can contain the // abbreviation.

Examples of match patterns:

<xsl:template match="table">

can be applied on any element named table.

<xsl:template match="x/y">

can be applied on any element named y whose parent is an element named x.

<xsl:template match="*">

can be applied to any element.

<xsl:template match="/*">

can be applied only to the top element of an XML document.

<xsl:template match="@*">

can be applied to any attribute.

<xsl:template match="text()">

can be applied to any text node.

<xsl:template match="comment()">

can be applied to any comment node.

<xsl:template match="processing-instruction()">

can be applied to any processing instruction node.

<xsl:template match="node()">

can be applied to any node: element, text, comment or processing instructon.

慈悲佛祖 2024-09-14 00:57:29

值得注意的是,XML 文档的根(或文档节点)不是顶级元素,这对于刚接触 XML 的人来说是很困惑的。它是顶级元素的父元素。这很令人困惑,因为顶级元素似乎不能有父元素。这不是顶级的吗?

但看看这个,一个格式良好的 XML 文档:

<?xml-stylesheet href="my_transform.xsl" type="text/xsl"?>
<!-- Comments and processing instructions are XML nodes too, remember. -->
<TopLevelElement/>

该文档的根具有三个子元素:处理指令、注释和元素。

因此,例如,如果您想编写一个删除该注释的转换,但保留文档中其他任何位置出现的任何注释,则可以将其添加到身份转换中:

<xsl:template match="/comment()"/>

甚至更简单(并且更常用),下面是一个 XPath 模式,它匹配文档的顶级元素,无论其名称如何:/*

It's worth noting, since it's confusing for people new to XML, that the root (or document node) of an XML document is not the top-level element. It's the parent of the top-level element. This is confusing because it doesn't seem like the top-level element can have a parent. Isn't it the top level?

But look at this, a well-formed XML document:

<?xml-stylesheet href="my_transform.xsl" type="text/xsl"?>
<!-- Comments and processing instructions are XML nodes too, remember. -->
<TopLevelElement/>

The root of this document has three children: a processing instruction, a comment, and an element.

So, for example, if you wanted to write a transform that got rid of that comment, but left in any comments appearing anywhere else in the document, you'd add this to the identity transform:

<xsl:template match="/comment()"/>

Even simpler (and more commonly useful), here's an XPath pattern that matches the document's top-level element irrespective of its name: /*.

心病无药医 2024-09-14 00:57:29

match 属性指示模板转换将应用于哪些部分。在这种特殊情况下,“/”表示 xml 文档的根。您必须提供给 match 属性的值应该是 XPath 表达式。 XPath 是您必须用来引用目标 xml 文件的特定部分的语言。

为了有意义地理解您还可以将哪些内容放入匹配属性中,您需要了解 Xpath 是什么以及如何使用它。我建议您查看我在答案底部为您提供的链接。

我可以编写 table 或任何其他 html 标记来代替“/”吗?

是的,你可以。但这取决于您到底想做什么。如果您的目标 xml 文件包含 HTML 元素,并且您尝试在其上应用此 xsl:template,则使用 tablediv 是有意义的或其他任何东西。

这里有一些链接:

The match attribute indicates on which parts the template transformation is going to be applied. In that particular case the "/" means the root of the xml document. The value you have to provide into the match attribute should be a XPath expression. XPath is the language you have to use to refer specific parts of the target xml file.

To gain a meaningful understanding of what else you can put into match attribute you need to understand what Xpath is and how to use it. I suggest you look at links I've provided for you at the bottom of the answer.

Could I write table or any other html tag instead of "/" ?

Yes you can. But this depends on what exactly you are trying to do. If your target xml file contains HTML elements and you are trying to apply this xsl:template on them, it makes sense to use table, div or anything else.

Here a few links:

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