xml XSL xsl:template match="/"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3127108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:11:03  来源:igfitidea点击:

XSL xsl:template match="/"

xmlxsltxslt-1.0xslt-2.0

提问by Pat

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

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

xsl:template match="/"

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

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

回答by Dimitre Novatchev

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

指令的match属性值<xsl:template>必须是匹配模式

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.

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

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

/在 XPath 中表示根节点或文档节点。在 XPath 2.0(以及 XSLT 2.x)中,这也可以写成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.

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

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

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

可以应用于任何名为y的元素,其父元素是名为 的元素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.

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

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

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

回答by Robert Rossney

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?

值得注意的是,对于 XML 的新手来说,XML 文档的根(或文档节点)不是顶级元素,这会让他们感到困惑。它是顶级元素的父元素。这令人困惑,因为顶级元素似乎没有父元素。不是顶级的吗?

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

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

<?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: /*.

更简单(也更常用),这里有一个 XPath 模式,它匹配文档的顶级元素,而不管其名称如何: /*.

回答by Koynov

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 XPath expression. XPath is the language you have to use to refer specific parts of the target xml file.

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

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 yo look at links I've provided for youat the bottom of the answer.

要对还可以放入 match 属性的内容有一个有意义的了解,您需要了解什么是 xpath 以及如何使用它。我建议您查看我在答案底部为您提供的链接。

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

我可以写“table”或任何其他html标签而不是“/”吗?

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

是的你可以。但这取决于您究竟要做什么。如果您的目标 xml 文件包含 HMTL 元素并且您正在尝试将这个 xsl:template 应用于它们,那么使用 table、div 或其他元素是有意义的。

Here a few links:

这里有几个链接: