A concise tutorial on XSL (2) XSL Transformation _xml/rss

Source: Internet
Author: User
Tags xsl xsl file xsl stylesheet
Original: A Egil refsnes translation: Atzie

Two. Transformation of XSL


1. Convert XML to HTML

How does XSL convert an XML document into an HTML file? Let's take a look at an example where the following is part of an 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>

...


We then convert the XML data to an HTML file as an HTML template for the following XSL file:


<?xml version= ' 1.0 '?>

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

<xsl:template match= "/" >


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


</xsl:template>

</xsl:stylesheet>


In the above code, the role of the Xsl:for-each element is to locate which elements in an XML document need to be displayed as the following template. The Select property is used to define the element name in the source file. This syntax for specifying attributes is also known as XML
Pattern (mode), similar to a representation of a subdirectory of a file. The xsl:value-of element is used to insert the content template of a child element in the current hierarchy.

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

If you add an XSL stylesheet to an XML document, look at line 2nd of the following code, and your browser can accurately convert the XML document into 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.