Easy Introduction to XSLT Chapter III: The element syntax _xml/rss of XSLT

Source: Internet
Author: User
Tags xsl xsl file xslt

With the introduction of the previous two chapters, we have some understanding of the basic concepts of XSLT and its transformation process. Let's learn the specific syntax of XSLT together. When it comes to grammar, it's always boring, and you can go through it in general, and then look at it when you really need to use XSLT.

element syntax for 3.XSLT

3.1 Xsl:template and Xsl:apply-templates

3.2 xsl:value-of

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 (template) is one of the most important concepts in XSLT. An XSLT file is made up of one template, and 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 consider a template as a method, a class, or a module. They can be assembled or individually grouped, and different templates control different output formats.

The template (template) consists of two parts: match pattern and execution. The simple schema defines which nodes in the XML source document will be processed by the template, and the execution defines what format the output is. The corresponding syntax for two parts is xsl:template and xsl:apply-templates.


The syntax for xsl:template is:


<xsl:template

Match = pattern

Name = QName

Priority = Number

mode = qname>

<!--executive Content-->

</xsl:template>


The role of Xsl:template is to define a new template. property, and mode is used to distinguish different templates that match the same node. \ name,priority They are not commonly used properties. The Match property controls the template's matching mode (pattern), which is used to locate which node in the XML source document is being processed by the template. A template matches a node. We use an example to help understand:

Let's say we're working with a chapter and paragraph document. We define the paragraphs with the Para element and define the chapters with the chapter elements. Let's look at the possible values for the match attribute. The following statement describes the template matching all para elements


<xsl:template match= "Para" >

</xsl:template>


The following statement illustrates that the template matches all the para elements and all the chapter elements:


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

</xsl:template>


The following statement describes the template that matches all para elements that are chapter elements of the parent node:


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

</xsl:template>


The following statement describes the template matching root node:


<xsl:template match= "/" >

</xsl:template>


Let's take another look at Apply-templates syntax:


<xsl:apply-templates

select = Node Set-expression

mode = qname>

</xsl:apply-templates>


Xsl:apply-templates is used to perform a node that is specifically handled by a template. You can interpret it as calling a child function in a program. The Select property is used to define the exact node name. Xsl:apply-templates are always included in the xsl:template element, like this:


<xsl:template match= "/" >

<xsl:apply-templates select= "Para"/>

</xsl:template>


This code demonstrates that the trackpad matches the entire document (the root node), and that it handles all the para elements under the root node when executed.


<xsl:template match= "Para" >

<p><xsl:apply-templates/></p>

</xsl:template>


This section of code means that the touchpad matches the Para node, and all the child elements under Para are processed.

3.2 xsl:value-of


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 with a profile:


<?xml version= "1.0" encoding= "Iso-8859-1"?>

<PERSON>

<name>ajie</name>

<age>28</age>

</PERSON>


If I want to display the value of the name element in the XML source document above in the output document, you can write the XSLT code like this:


<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 touch board to match the person node, xsl:value-of
The syntax description needs to output the value of one node, while select= "name" defines the element that needs to be exported as name. Does this process look like a person's name in a database? Of course, the xsl:value-of query has more and more complex syntax, because it involves finding and positioning functions, and we'll put them in the later XPath syntax.

The same function is also xsl:copy-of, use the same, do not repeat the explanation.

3.3 Xsl:for-each


The Xsl:for-each syntax allows you to iterate over the selected node. For example, there is an XML document that contains multiple profiles:


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


I need to display the names of everyone, you can write the XSLT code:


<xsl:template match= "People" >

<xsl:for-each select= "Child::P Erson" >

<xsl:value-of select= "Name"/>

</xsl:for-each>

</xsl:template>


3.4 xsl:if


Xsl:if is similar to the common programming language if condition statement, allows the set node to satisfy a certain condition, is processed by the template. The syntax format for 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 means to detect all elements under the People node and, if a <name> element is found, outputs the <name> element's value. Where the @ symbol is the wildcard character, representing all the elements under the node.

3.5 Xsl:choose, Xsl:when and Xsl:otherwise


The XSL:IF syntax does not have an else attribute. If we are going to make multiple choices, we need to use the Xsl:choose/xsl:when/xsl:otherwise series Process Control syntax. For specific use, 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>


Description: First in the People node to find <name> attribute value of Ajie elements, if found, will ajie in bold output; If you do not find a <name> element with a value of Ajie, all <name> The value of the element is output in italics, and if no <name> element is found, the "no" is displayed.
Name available ".


3.6 Xsl:sort


In XSLT, the elements of an XML source document can be reordered, and the ordering syntax is xsl:sort. For example: The following code is to sort the document elements by name.


<xsl:template match= "People" >

<xsl:apply-templates select= "Person" >

<xsl:sort select= "@name"/>

</xsl:apply-templates>

</xsl:template>


These are the main syntaxes of the elements of XSLT, and many other syntaxes, such as import, include, element, attribute, number, param, and so on, are not explained here. Our aim is to give you a basic idea of the syntax of XSLT, and to understand XSLT as a powerful function of translating languages.

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.