Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XSLT conversion


May 28, 2021 XSL T


Table of contents


XSLT - Conversion

XSLT is a language used to transform the structure of XML documents, and this section teaches you to convert XML documents to XHTML by example.

Example study: How to convert XML to XHTML using XSLT.

We'll explain the details of this example in the next chapter.


The correct style sheet declaration

The root element that declares the document as an XSL style sheet is slt;xsl:stylesheets;or slt;xsl:transform>.

Note: slt;xsl:stylesheet and slt;xsl:transform> are completely synonymable and can be used!

According to W3C's XSLT standard, the correct way to declare an XSL style sheet is to:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Or:

<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

To access the elements, properties, and attributes of XSLT, we must declare the XSLT namespace at the top of the document.

xmlns:xsl""http://www.w3.org/1999/XSL/Transform""" If you use this namespace, you must include the property version "1.0".


Start with an original XML document

Let's now convert the following XML document ("cdcatalog.xml") to XHTML:

<?xml version="1.0" encoding="ISO-8859-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>
.
.
</catalog>

View XML files in Firefox and Internet Explorer: Open an XML file (usually by clicking on a link) - The XML document displays the root and child elements in colored code. C licking on the plus sign to the left of the element or minus sign (-) expands or shrinks the structure of the element. To view the original XML source file without plus or minus sign, select View Page Source code or View Source Code in the browser menu.

View the XML file in Netscape 6: Open the XML file, then right-click on the XML file and select View page source code. XML documents display root and child elements in colored code.

View the XML file in Opera 7: Open the XML file, then right-click on the XML file, select Frame/View Source Code. The XML document appears as plain text.

View "cdcatalog.xml"


Create an XSL style sheet

Then create an XSL style sheet with a conversion template ("cdcatalog.xsl"):

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<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>

Link the XSL style sheet to the XML document

Add an XSL style sheet reference .xml XML document ("cdcatalog"):

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.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>
.
.
</catalog>

If you're using a browser that's compatible with XSLT, it's going to convert your XML to XHTML very smoothly.

View the results

In the next section, we'll cover the examples that appear in this section in more detail!