XSL concise tutorial (2) XSL Conversion

Source: Internet
Author: User
Tags xsl file

Original: Jan Egil Refsnes

Ii. XSL Conversion

1. Convert XML into HTML

How does XSL convert XML files into HTML files? Let's look at an example. below is part of the XML document:

<? Xml version = "1.0" encoding = "ISO8859-1"?>

<CATALOG>

<CD>

<TITLE> Empire Burlesque </TITLE>

<ARTIST> Bob Dylan </ARTIST>

<COUNTRY> USA </COUNTRY>

<COMPANY> Columbia </COMPANY>

<PRICE> 10.90 </PRICE>

<YEAR> 1985 </YEAR>

</CD>

...

Then, we use the following XSL file as an HTML template to convert XML data to an HTML file:

<? Xml version = '1. 0'?>

<Xsl: stylesheet xmlns: xsl = "http://www.w3.org/TR/WD-xsl">

<Xsl: template match = "/">

<Html>

<Body>

<Table border = "2" bgcolor = "yellow">

<Tr>

<Th> Title </th>

<Th> Artist </th>

</Tr>

<Xsl: for-each select = "CATALOG/CD">

<Tr>

<Td> <xsl: value-of select = "TITLE"/> </td>

<Td> <xsl: value-of select = "ARTIST"/> </td>

</Tr>

</Xsl: for-each>

</Table>

</Body>

</Html>

</Xsl: template>

</Xsl: stylesheet>

In the above Code, the xsl: for-each element is used to locate which elements in the XML document need to be displayed according to the following template. The select attribute is used to define the element name in the source file. This syntax for specifying attributes is also called XML
Pattern, similar to the representation of file subdirectories. The xsl: value-of element is used to insert the content template of child elements in the current level.

Because the XSL style sheet itself is also an XML document, the beginning of the XSL file begins with an XML declaration. Xsl: the stylesheet element is used to declare that this is a style sheet file. <Xsl: template
Match = "/"> statement indicates that the XML source file is in the current directory.

If you add an XSL style table to an XML document, you can see the following code in line 2. Your browser can accurately convert the XML document to an HTML file.

<? Xml version = "1.0" encoding = "ISO8859-1"?>

<? Xml-stylesheet type = "text/xsl" href = "cd_catalog.xsl"?>

<CATALOG>

<CD>

<TITLE> Empire Burlesque </TITLE>

<ARTIST> Bob Dylan </ARTIST>

<COUNTRY> USA </COUNTRY>

<COMPANY> Columbia </COMPANY>

<PRICE> 10.90 </PRICE>

<YEAR> 1985 </YEAR>

</CD>

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.