Two.XSL conversion
1. Convert XML to HTML
How does XSL convert XML documents into HTML files? Let's look at an example, and the following is a 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 convert the XML data into HTML file as a template for the following XSL file:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http:///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 function of the xsl:for-each element is to locate which elements in the XML document need to be displayed in 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 a file subdirectory. The xsl:value-of element is used to insert the content template of child elements in the current level.
Because the XSL stylesheet itself is also an XML document, the beginning of the XSL file starts with an XML declaration. The xsl:stylesheet element is used to declare this as a stylesheet file. <xsl:template
The match="/"> statement indicates that the source document of the XML is in the current directory.
If you add an XSL stylesheet to an XML document, look at line 2 of the code below, and 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>