XSLT - Transformation
Case Study: How to Use XSLT to Transform XML into XHTML.
We will explain the details of this example in the next chapter.
Correct Stylesheet Declaration
The root element that declares the document as an XSL stylesheet is <xsl:stylesheet>
or <xsl:transform>
.
Note: <xsl:stylesheet>
and <xsl:transform>
are completely synonymous and can be used interchangeably!
According to the W3C XSLT standard, the correct way to declare an XSL stylesheet is:
Or:
To access the elements, attributes, and features of XSLT, we must declare the XSLT namespace at the top of the document.
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
points to the official W3C XSLT namespace. If you use this namespace, you must include the attribute version="1.0"
.
Starting with a Raw XML Document
We are now going to transform the following XML document ("cdcatalog.xml") into XHTML:
Viewing the XML File in Firefox and Internet Explorer: Open the XML file (usually by clicking a link) - The XML document will be displayed with colored code for the root element and child elements. Click the plus (+) or minus (-) sign on the left of the elements to expand or collapse the element structure. To view the raw XML source file (without plus and minus signs), select "View Page Source" or "View Source" from the browser menu.
Viewing the XML File in Netscape 6: Open the XML file, right-click within the XML file, and select "View Page Source". The XML document will be displayed with colored code for the root element and child elements.
Viewing the XML File in Opera 7: Open the XML file, right-click within the XML file, and select "Frame"/"View Source". The XML document will be displayed as plain text.
Creating an XSL Stylesheet
Then create an XSL stylesheet ("cdcatalog.xsl") with a transformation template:
Linking the XSL Stylesheet to the XML Document
Add an XSL stylesheet reference to the XML document ("cdcatalog.xml"): <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
If your browser is compatible with XSLT, it will smoothly transform your XML into XHTML.
We will explain the details of the above example in the next chapter.