XSL xsl: template match = "/"

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

xsl:template match="/" 

What does it mean? And what could I use instead of / ? Can I write table or any other HTML tag instead of / ?

+52
Jun 27 2018-10-06T00:
source share
3 answers

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

Matching patterns form a subset of the set of all possible XPath expressions . The first natural limitation is that the mapping pattern must select a set of nodes. There are other limitations. In particular, inverse axes are not allowed in the location steps (but can be specified in predicates). In addition, variables or parameters are not allowed in XSLT 1.0, but their use is permitted in XSLT 2.x.

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

The match pattern may contain the abbreviation // .

Examples of match patterns:

 <xsl:template match="table"> 

can be applied to any element named table .

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

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

 <xsl:template match="*"> 

can be applied to any element.

 <xsl:template match="/*"> 

can only be applied 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 node of the processing instruction.

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

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

+119
Jun 27 '10 at 16:10
source share

It is worth noting since this confuses for people new to XML that the root (or document node) of an XML document is not a top-level element. This is the parent of the top level. This is confusing because it does not look like a top-level element can have a parent. Isn't that 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 consists of three child elements: a processing command, a comment, and an element.

So, for example, if you want to write a transformation that gets rid of this comment but is left in any comments appearing elsewhere in the document, you should add this to the identity transformation:

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

Even simpler (and more useful), here is the XPath template corresponding to the top-level element of the document, regardless of its name: /* .

+40
Jun 28 '10 at 16:20
source share

The match attribute indicates which parts the template transform will apply to. In this particular case, "/" means the root of the XML document. The value you must specify in the match attribute must be an XPath expression. XPath is a language that you should use to refer to specific parts of the target XML file.

To get a meaningful idea of ​​what else you can insert into the matching attribute, you need to understand what xpath is and how to use it. I suggest you look at the links that I provided to you at the bottom of the answer.

Can I write a β€œtable” or any other HTML tag instead of β€œ/”?

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

Here are some links:

+8
Jun 27 2018-10-06T00:
source share



All Articles