XSLT syntax and functions

Source: Internet
Author: User
Tags processing instruction xsl xslt

4.3.4 XSLT syntax and functions

In the previous example, I believe that the reader has probably understood the general form and functions of XSLT. In this section, we will discuss it comprehensively.

    1. Document Structure

As mentioned above, the XSLT document itself is an XML document, so the first sentence of the document is naturally:

<? XML version = "1.0"?>

Next is the style sheet section:

<XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

... ...

</XSL: stylesheet>

You can also write:

<XSL: Transform version = "1.0" xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

... ...

</XSL: Transform>

XSL: transform has the same meaning as XSL: stylesheet, indicating that the content contained in the element is a style sheet. XSL: the stylesheet element must contain the "version" attribute to indicate which version of the XSL document complies with the XSL standard. In addition, xmlns: XSL indicates the namespace of XSL. In the XSLT standard, the namespace of XSLT is defined as http://www.w3.org/5o/#/transform.

When XSLT is transforming, it first traverses the XML Source Document Tree, finds the node to be processed, and then applies the defined template information to the node.

    1. Templates and Applications

XSL: Template is a template element. Generally, each XSL: Template has a node matching attribute specified by "match =. When matching a template, use "XSL: Apply-templates" and select the template to be matched, which is equivalent to a call process. The node matching rules follow XPath.

Different template designs can lead to different output effects for the same document. XSL: The template element has a mode attribute, which can match templates of different modes as needed. If you modify the statement as follows:

<XSL: template match = "/" mode = "blue">

...

<Title> student roster </title>

<Style>. Title {font-size: 15pt; font-weight: bold; color: Blue}

...

<XSL: template match = "/" mode = "red">

...

<Title> student roster </title>

<Style>. Title {font-size: 15pt; font-weight: bold; color: Red}

...

If you want to output the title in blue, use the following statement to match:

<XSL: Apply-templates select = "/" mode = "blue"/>

If you want to output the title in red, write it:

<XSL: Apply-templates select = "/" mode = "red"/>

In addition, templates always correspond to nodes. A node may correspond to different templates. How can we determine the order of template matching? In XSLT, priority can be set for XSL: Template. The statement is as follows:

<XSL: template match = "student" priority = "N"> // n indicates the priority level.

    1. Compute node Value

When using XSLT for conversion, you often need to obtain the node value. You can use the XSL: value-of element to achieve this purpose, as shown in the following example:

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

The obtained value is the student's original value. The select attribute specifies the node value of the node to be obtained.

    1. Loop Processing

You can use XSL: For-each to process the selected nodes in sequence. In this example, in the process of generating a table, the information of each student is retrieved and put into the table cyclically. The statement is as follows:

<XSL: For-each select = "student" Order-by = "name">... </XSL: For-each>

    1. Sort

For nodes that match with XSL: For-each or XSL: Apply-templates, you can use XSL: sort to sort the content of the selected nodes, for example:

Sorting Method

For example

Meaning

Sort by case <XSL: Sort case-order = "Upper-First" select = "@ ID"/> Sort by uppercase with ID as the keyword first
<XSL: Sort case-order = "lower-First" select = "@ ID"/> Sort by lower case with ID as the keyword first
Sort by letter <XSL: sort order = "ASCENDING" select = "@ ID"/> Sort by letter in ascending order with ID as the keyword
<XSL: sort order = "descending" select = "@ ID"/> Sort by letter in descending order with ID as the keyword
Sort by Data Type <XSL: Sort data-type = "text" select = "@ ID"/> Sort by text with ID as the keyword. For example, for a group of ID data of 44,305 and, the sorting result is, and 44.
<XSL: Sort data-type = "Number" select = "@ ID"/> Sort by data type with ID as the keyword. The sorting result of the above group of data is 101,305

In addition, there is also a way to specify the sort, that is, the order-by used in the previous student roster example:

<XSL: For-each select = "student" Order-by = "name">

You can also sort the output students by names.

    1. Element and attribute Creation

XSLT is a dynamic style sheet that can generate new elements or element attributes during processing. The method is as follows:

Internal capacity Yuansu For example Conversion Result
Create Element XSL: Element <XSL: element name = "title">

Student roster

</XSL: Element>
<Title> student roster </title>
Create attributes XSL: attribute <Title>

<XSL: attribute name = "style">

Color: Blue

</XSL: attribute>

Student roster

</Title>
<Title style = "color: Blue">

Student roster

</Title>
Create text XSL: Text

(White spaces in text can be protected)
<XSL: Text>

This is the student roster.

</XSL: Text>
Output text:

This is the student roster.
Create Processing Instruction XSL: Processing-Instruction <XSL: Processing-instruction name = "XML-stylesheet">

Href = "book.css"

Type = "text/CSS"

</XSL: Processing-instruction>
<? XML-stylesheet href = "book.css" type = "text/CSS"?>
Create comment XSL: Comment <XSL: Comment>

The following is a student roster. Do not delete it!

</XSL: Comment>
<! -- The following is a student roster. Do not delete it! -->

    1. Node copy

When processing XML documents, XSLT can also copy nodes by using XSL: Copy and XSL: copy-. XSL: copy only copies the current node, excluding the child nodes and attributes. The copy content of XSL: copy-of includes the current node, child nodes, and attributes. For example:

<P id = "p1"> A <B> is a char </B> </P>

If the style sheet is written in the following format:

<XSL: stylesheetversion = "1.0"

Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

<XSL: template match = "p">

<Div>

<XSL: Text> copy-of: </XSL: Text>

<XSL: copy-of select = "."/>

</Div>

<Div>

<XSL: Text> copy: </XSL: Text>

<XSL: copy/>

</Div>

</XSL: Template>

</XSL: stylesheet>

The conversion result is as follows:Code:

<Div>

Copy-of: <p id = "p1"> A <B> is a char </B> </P>

</Div>

<Div>

Copy: <p/>

</Div>

It can be seen that the two copy methods have very different results.

    1. Output Format and encoding

XSLT is a conversion language that converts an XML source document to another format document. The output result can be an HTML document or an XML document with CSS. The specific output format is specified by XSL: output. If you want to output an HTML document, write it:

<XSL: output method = "html"/>

Similarly, the output XML document is written as follows:

<XSL: output method = "XML"/>

If XSL: output is not displayed in the document, the XML document is output by default. However, if the <HTML> tag is used to match the template, the HTML document is output. When the output is an HTML document, the system automatically adds the following statement:

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">

In addition, you can also use XSL: output to specify the encoding method, such as UTF-8, UTF-16, gb2312 and so on. For example:

<XSL: output method = "html" encoding = "gb2312"/>

It specifies that the output result of this XSLT is in HTML format and the encoding method is Chinese.

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.