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>