Chapter 3 XSLT element syntax

Source: Internet
Author: User
Tags xsl xsl file xslt

Through the introduction in the previous two chapters, we have learned some about the basic concepts of XSLT and its conversion process. Next we will learn the specific syntax of XSLT. When it comes to syntax, it's always boring. You can simply browse it and study it carefully when you really need XSLT.

3. Element Syntax of XSLT

3.1 XSL: Template and XSL: Apply-templates

3.2 XSL: value-

3.3 XSL: For-each

3.4 XSL: If

3.5 xxsl: Choose, when, otherwise

3.6 XSL: Sort

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.ProgramYou 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 control different output formats.

A template consists of two parts: Match pattern and execution. The simple mode defines which node in the XML source document will be processed by the template, and the execution defines the output format. The syntax of the two parts is XSL: Template and XSL: Apply-templates.

The syntax of XSL: Template is:

<XSL: Template

Match = Pattern

Name = QNAME

Priority = Number

Mode = QNAME>

<! -- Execution content -->

</XSL: Template>

XSL: Template defines a new template. The name, priority, and mode in the attribute are used to distinguish different templates matching 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 us 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 following statement describes that the template matches all the Para elements.

<XSL: template match = "para">

</XSL: Template>

The following statement describes that the template matches all para elements and all Chapter elements:

<XSL: template match = "(Chapter | para)">

</XSL: Template>

The following statement indicates that the template matches all the Para elements whose parent nodes are Chapter elements:

<XSL: template match = "Chapter // para">

</XSL: Template>

The following statement describes that the template matches the root node:

<XSL: template match = "/">

</XSL: Template>

Let's take a look at the apply-templates Syntax:

<XSL: Apply-templates

Select = node set-expression

Mode = QNAME>

</XSL: Apply-templates>

XSL: Apply-templates is used to execute the node to be processed by the template. You can understand it as calling subfunctions in a program. The select attribute defines the exact node name. XSL: Apply-templates is always included in the XSL: Template element, like this:

<XSL: template match = "/">

<XSL: Apply-templates select = "para"/>

</XSL: Template>

This sectionCodeThe instruction board matches the entire document (root node) and processes all the Para elements under the root node during execution.

<XSL: template match = "para">

<P> <XSL: Apply-templates/> </P>

</XSL: Template>

This Code indicates that the touchpad matches the Para node, and all child elements under para are processed.

3.2 XSL: value-

XSL: value-of is used to write the text values of elements in the source document to the output document. For example:

There is an XML document for personal data:

<? XML version = "1.0" encoding = "iso-8859-1"?>

<Person>

<Name> Ajie </Name>

<Age> 28 </age>

</Person>

If you want to display the name element value in the above XML source document in the output document, you can write the XSLT Code as follows:

<XSL: template match = "person">

<XSL: value-of select = "name"/>

</XSL: Template>

After execution, you will see that "Ajie" is displayed separately. Where match = "person" defines the pattern board to match the person node, XSL: value-
Syntax description: outputs the value of a node, while select = "name" defines the element to be output as name. Check whether this process is similar to querying a person's name in the database? Of course, there are more and more complex syntaxes for XSL: value-of queries. because it involves the searching and locating functions, we will explain them carefully in the later XPath syntax.

The same functions include XSL: copy-of. If the usage is the same, it will not be repeated.

3.3 XSL: For-each

XSL: For-each syntax allows you to process selected nodes cyclically. For example, there is an XML document containing multiple personal data:

<? XML version = "1.0" encoding = "iso-8859-1"?>

<People>

<Person>

<Name> Ajie </Name>

<Age> 28 </age>

</Person>

<Person>

<Name> Tom </Name>

<Age> 24 </age>

</Person>

<Person>

<Name> miake </Name>

<Age> 30 </age>

</Person>

</People>

To display the names of all people, you can write the XSLT Code as follows:

<XSL: template match = "people">

<XSL: For-each select = "Child: Person">

<XSL: value-of select = "name"/>

</XSL: For-each>

</XSL: Template>

3.4 XSL: If

XSL: If is similar to the IF Condition Statement in common programming languages. If a node can be set to meet a certain condition, it is processed by a template. The syntax format of XSL: If is:

<XSL: If test = Boolean expression>

Template body

</XSL: If>

For example:

<XSL: template match = "people">

<XSL: If test = "@ name">

<P> <XSL: value-of select = "@ name"/> </P>

</XSL: If>

</XSL: Template>

This code detects all elements under the People node. If a <Name> element is found, the value of the <Name> element is output. The @ symbol indicates all elements under the node.

3.5 XSL: Choose, XSL: When and XSL: otherwise

XSL: The if syntax does not have the attributes of else. If you want to make multiple choices, you need to use the XSL: Choose/XSL: When/XSL: otherwise series process control syntax. For specific usage, see the following XSL file example:

<XSL: template match = "people">

<XSL: Choose>

<XSL: When test = "@ name = 'ajie '">

<B> <XSL: value-of select = "@ name"/> </B>

</XSL: When>

<XSL: When test = "@ name">

<I> <XSL: value-of select = "@ name"/> </I>

</XSL: When>

<XSL: otherwise>

No name available

</XSL: otherwise>

<XSL: Choose>

</XSL: Template>

Note: First, find the elements whose <Name> attribute value is Ajie under the People node. If yes, output Ajie in bold. If no <Name> element whose value is Ajie is found, all <Name> element values are output in italic. If no <Name> element is found, "no" is displayed.
Name available ".

3.6 XSL: Sort

In XSLT, elements of the XML source document can be re-ordered. The sorting syntax is XSL: sort. For example, the following code sorts document elements by name.

<XSL: template match = "people">

<XSL: Apply-templates select = "person">

<XSL: Sort select = "@ name"/>

</XSL: Apply-templates>

</XSL: Template>

The above are the main syntaxes of XSLT elements, and there are many other syntaxes, such as import, include, element, attribute, number, And Param, which will not be explained here. Our goal is to give you a basic concept of XSLT syntax and understand the powerful functions of XSLT as a conversion language.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.