XSL:“xsl:template”的“match=”/“”的含义
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
指令的match
属性值必须是匹配模式。匹配模式构成所有可能的 XPath 表达式集合的子集。第一个自然的限制是匹配模式必须选择一组节点。还有其他限制。特别是,位置步骤中不允许使用反向轴(但可以在谓词中指定)。此外,XSLT 1.0 中不允许使用变量或参数引用,但在 XSLT 2.x 中使用它们是合法的。
XPath 中的
/
表示根或文档节点。在 XPath 2.0(以及 XSLT 2.x)中,这也可以写为document-node()
。匹配模式可以包含
//
缩写 。匹配模式示例:
可以应用于任何名为
table
的元素。可以应用于任何名为
y
的元素,其父元素是名为x
的元素。可以应用于任何元素。
只能应用于 XML 文档的顶部元素。
可以应用于任何属性。
可以应用于任何文本节点。
可以应用于任何评论节点。
可以应用于任何处理指令节点。
可以应用于任何节点:元素、文本、注释或处理指令。
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 asdocument-node()
.A match pattern can contain the
//
abbreviation.Examples of match patterns:
can be applied on any element named
table
.can be applied on any element named
y
whose parent is an element namedx
.can be applied to any element.
can be applied only to the top element of an XML document.
can be applied to any attribute.
can be applied to any text node.
can be applied to any comment node.
can be applied to any processing instruction node.
can be applied to any node: element, text, comment or processing instructon.
值得注意的是,XML 文档的根(或文档节点)不是顶级元素,这对于刚接触 XML 的人来说是很困惑的。它是顶级元素的父元素。这很令人困惑,因为顶级元素似乎不能有父元素。这不是顶级的吗?
但看看这个,一个格式良好的 XML 文档:
该文档的根具有三个子元素:处理指令、注释和元素。
因此,例如,如果您想编写一个删除该注释的转换,但保留文档中其他任何位置出现的任何注释,则可以将其添加到身份转换中:
甚至更简单(并且更常用),下面是一个 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:
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:
Even simpler (and more commonly useful), here's an XPath pattern that matches the document's top-level element irrespective of its name:
/*
.match 属性指示模板转换将应用于哪些部分。在这种特殊情况下,“/”表示 xml 文档的根。您必须提供给 match 属性的值应该是 XPath 表达式。 XPath 是您必须用来引用目标 xml 文件的特定部分的语言。
为了有意义地理解您还可以将哪些内容放入匹配属性中,您需要了解 Xpath 是什么以及如何使用它。我建议您查看我在答案底部为您提供的链接。
是的,你可以。但这取决于您到底想做什么。如果您的目标 xml 文件包含 HTML 元素,并且您尝试在其上应用此
xsl:template
,则使用table
、div
是有意义的或其他任何东西。这里有一些链接:
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.
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 usetable
,div
or anything else.Here a few links: