Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XSLT Transformation

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.


The Correct Stylesheet Declaration

The root element that declares a document as an XSL stylesheet is xsl:stylesheet or xsl:transform。

Note:xsl:stylesheet and xsl:transform are completely synonymous and both can be used!

According to the W3C XSLT standard, the correct way to declare an XSL stylesheet is:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

To access XSLT elements, attributes, and features, 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".


Start with a Raw XML Document

We now want to transform the following XML document ("cdcatalog.xml")Transforminto XHTML:

Empire Burlesque

Bob Dylan

USA

Columbia

10.90

1985

.

.

**Viewing XML files in Firefox and Internet Explorer:** Open the XML file (usually by clicking a link) - the XML document displays the root element and child elements as colorized code. Click the plus (+) or minus (-) sign to the left of an element to expand or collapse its structure. To view the raw XML source (without the plus and minus signs), select "View Page Source" or "View Source" from the browser menu.

**Viewing XML files in Netscape 6:** Open the XML file, right-click in the XML file, and select "View Page Source". The XML document displays the root element and child elements as colorized code.

**Viewing XML files in Opera 7:** Open the XML file, right-click in the XML file, and select "Frame"/"View Source". The XML document is displayed as plain text.

View "cdcatalog.xml"


Create the XSL Stylesheet

Then create an XSL stylesheet with a transformation template ("cdcatalog.xsl"):

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My CD Collection

Title Artist

View "cdcatalog.xsl"


Link the XSL Stylesheet to the XML Document

Add the XSL stylesheet reference to the XML document ("cdcatalog.xml"):

Empire Burlesque

Bob Dylan

USA

Columbia

10.90

1985

.

.

If your browser is XSLT-compatible, it will smoothly transform your XML Transforminto XHTML.

View the result

We will explain the details of the example above in the next chapter.