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>