3.1xsl: template and xsl: apply-templates template are one of the most important concepts in XSLT. An XSLT file is composed of one template. any XSLT file contains at least one template. The concept of a template is like building blocks.
3.1 xsl: template and xsl: apply-templates
Template is one of the most important concepts in XSLT. An XSLT file is composed of one template. any XSLT file contains at least one template. The concept of a template is like building blocks. if you are a programmer, you can also think of a template as a method, a class, or a module. They can be assembled and combined, or separate into blocks. different templates hold different output patterns.
A template consists of two parts: match pattern and fulfillment. In brief, the mode defines which node in the XML source document will be processed by the template, and the performance defines the output pattern. The syntax of the two parts is xsl: template and xsl: apply-templates.
The syntax of xsl: template is:
Match = pattern
Name = qname
Priority = number
Mode = qname>
Xsl: template defines a new template. Name, priority, and mode in the attribute are used to match different templates of the same node. They are not common attributes. The match attribute controls the pattern of the template. the pattern is used to locate which node in the XML source document is processed by the template. A template matches a node. Let's use an example to help you understand:
Suppose we want to process a section document that contains chapters and paragraphs. We use the para element to define paragraphs and the chapter element to define chapters. Let's take a look at the possible values of the match attribute. The statement below clarifies that the template matches all para elements.
The statement below clarifies that the template matches all para elements and all chapter elements:
The statement below clarifies that the template matches all para elements whose parent nodes are chapter elements:
The statement below clarifies that the template matches the root node:
Let's take a look at the apply-templates syntax:
Select = node set-expression
Mode = qname>
Xsl: apply-templates is used to perform the specific processing of that node by the template. You can call subfunctions in a program. The select attribute is used to define a node name. Xsl: apply-templates is always included in the xsl: template element, like this:
This code clarifies that the TouchPad matches all documents (root nodes) and processes all the para elements under the root node during actual performance.
This piece of code matches the para node, and all child elements under para are processed.