Simplified XML tutorial (4)

Source: Internet
Author: User
Tags xml parser xsl xslt xslt example
 
Iv. Introduction to XSLT
It is far from enough to understand the basic syntax and Schema syntax of XML. XML is the storage of data and data structures, while schema is the definition of XML data types. Until this

XML data display is not processed yet. Although you can view XML data directly through a browser with an XML parser, after all, what you see is a tree structure.

The XML data displayed.

If you want to display XML data in a beautiful form, you need XSLT to help. XSLT can process XML into HTML output, WAP output, or even

Is the format of Word documents. The most commonly used format is the HTML format. All examples used in the following section use the HTML conversion format as an example. That's not nonsense.

Let's look at An XSLT example first!

The XML document used in this XSLT is as follows:

<? XML version = "1.0" encoding = "gb2312"?>

<? XML: stylesheet type = "text/XSL" href = "s_xlt.xslt"?>

<Resumes>

<Resume>

<Name> Zhang Lidong </Name>

<Sex> male </sex>

<Age> 28 </age>

<Birthday>

<Year> 1974 </year>

<Month> 8 </month>

<Day> 10 </day>

</Birthday>

<Address> Haidian District, Beijing </address>

</Resume>

<Resume>

<Name> Zhang San </Name>

<Sex> male </sex>

<Age> 50 </age>

<Birthday>

<Year> 1951 </year>

<Month> 6 </month>

<Day> 9 </day>

</Birthday>

<Address> Shanxi </address>

</Resume>

<Resume>

<Name> Li Si </Name>

<Sex> male </sex>

<Age> 29 </age>

<Birthday>

<Year> 1973 </year>

<Month> 7 </month>

<Day> 12 </day>

</Birthday>

<Address> Shandong Province </address>

</Resume>

</Resumes>

Note: In the preceding XML document, the following sentence is added:

<? XML: stylesheet type = "text/XSL" href = "s_xlt.xslt"?>

This statement references XSLT. The content in href specifies the location of a specific XSLT document. The content in s_xlt.xslt is the XSL

T. For how to use and test XML, XLST, and schema, see "an example of applying XML, schema, and XLST.

The XSLT document for converting this XML document is as follows:

[1] <? XML version = "1.0" encoding = "gb2312"?>

[2] <XSL: stylesheet version = "1.0" xmlns: XSL = "http://www.w3.org/TR/WD-xsl">

[3] <XSL: template match = "/">

[4] <HTML>

[5]

[6] <title> resume </title>

[7]

[8] <body>

[9] <XSL: For-each select = "/resumes/resume">

[10] <p>

[11] <Table border = "1">

[12] <caption style = "font-size: 150%; font-weight: bold">

[13] resume

[14] </caption>

[15] <tr>

[16] <TH> name </Th> <TD> <XSL: value-of select = "name"/> </TD>

[17] <TH> gender </Th> <TD> <XSL: value-of select = "sex"/> </TD>

[18] <TH> birthday </Th> <TD> <XSL: value-of select = "birthday/year"/> year

<XSL: value-of select = "birthday/Month"/> month <XSL: value-of select = "birthday/day"/> day </TD>

[19] </tr>

[20] <tr>

[21] <TH> address </Th> <TD colspan = "5"> <XSL: value-of select = "Address"/> </TD>

[22] </tr>

[23] </table>

[24] </XSL: For-each>

[25] </body>

[26]

[27] </XSL: Template>

[28] </XSL: stylesheet>

[1] preface to XML, we can see from the first statement that XSLT is also in line with the XML syntax.

[2] Name field of XSLT. XSL: stylesheet is the root element of XSLT, and XSL is the name field identifier of XSLT. Therefore, we must add XSL: Before the XSLT statement to indicate that it is XSLT.

.

[3] <XSL: template match = "/"> is the matching mode of the XSLT language. It mainly matches the root of the XML document that references it. Where/Represents the content of the XML document root

. For example, you can use/resumes/resume/name in the personal profile.

. (This is only a preliminary introduction to XSLT. For more information about XML paths, see the XPath section of the XML document .)

[4]-[8] can be directly output. This part is the information structure conforming to the HTML syntax, the starting part of the HTML document and the head definition.

[9] <XSL: For-each select = "/resumes/resume"> is the selection mode of the XSLT language, which cyclically applies to all/resumes/resume elements and

Contains sub-elements for processing.

[10]-[15] can be directly output and conform to the HTML syntax structure.

[16] <TH> name </Th> <TD> <XSL: value-of select = "name"/> </TD>

In this line, there are HTML statements and XSLT statements. <XSL: value-of select = "name"/> is also a select mode statement. You can directly select

After the processing, the result should be: <TH> name </Th> <TD> Zhang Lidong </TD>.

[18]-[23] displays other information, including some HTML statements.

[24] <XSL: For-each> end part of the statement.

[25]-[26] HTML statements.

[27] <XSL: Template> end part of the statement. That is, the end of processing the entire XML document. Because each XML document has only one root, and this statement is the root part.

Processing is the end of processing the entire XML document.

[28] End of the XSLT document.

After the introduction of this simple example, you may have a basic understanding of XSLT's processing of XML documents. You can simply use XSLT to process XML.

Processing. Make a simple summary of the XSLT syntax before the end of this section:

We can roughly divide the Model language into three types:

Select Mode

<XSL: For-each>, <XSL: value-of>, and <XSL: Apply-templates>

Test Mode

<XSL: If> and <XSL: When>

Matching Mode

<XSL: Template>

Select a schema statement to extract data from XML. This is a simple way to obtain data. These tags all have a select attribute. Select a specific knot in XML.

Name Data.

The test mode is to judge the data and process the data based on the judgment and matching results. Some are similar to the judgment statements and multiple judgment statements in the program language.

The matching mode is An XSLT statement segment that can be listed separately. It can process the XSLT of a specific part. Some are similar to subroutines in programming.

Related Article

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.