w3schools    w3Schools
Search W3Schools :
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About

XML Certification
Download XML editor
Custom Programming
Table of contents
XSLT Basic
XSL Languages
XSLT Introduction
XSLT Browsers
XSLT Transform
XSLT <template>
XSLT <value-of>
XSLT <for-each>
XSLT <sort>
XSLT <if>
XSLT <choose>
XSLT Apply

XSLT Advanced
XSLT on the Client
XSLT on the Server
XSLT Editors
XSLT Summary

XSLT Examples

XSLT Elements
XSLT Functions

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Tutorials
W3Schools Forum

Helping W3Schools


XSLT - On the Client

prev next

If your browser supports it, XSLT can be used to transform the document to XHTML in your browser.

A JavaScript Solution

In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.

Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.)

A more versatile solution would be to use a JavaScript to do the transformation.

By using a JavaScript, we can:

  • do browser-specific testing
  • use different style sheets according to browser and user needs

That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.

XSLT transformation on the client side is bound to be a major part of the browsers work tasks in the future, as we will see a growth in the specialized browser market (Braille, aural browsers, Web printers, handheld devices, etc.)

The XML File and the XSL File

Look at the XML document that you have seen in the previous chapters:

<?xml version="1.0" encoding="ISO-8859-1"?>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>

View the XML file.

And the accompanying XSL style sheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
<xsl:template match="/">
    <h2>My CD Collection</h2> 
    <table border="1">
      <tr bgcolor="#9acd32">
        <th align="left">Title</th> 
        <th align="left">Artist</th> 
      <xsl:for-each select="catalog/cd">
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="artist" /></td>

View the XSL file.

Notice that the XML file does not have a reference to the XSL file.

IMPORTANT: The above sentence indicates that an XML file could be transformed using many different XSL style sheets.

Transforming XML to XHTML in the Browser

Here is the source code needed to transform the XML file to XHTML on the client:

function loadXMLDoc(fname)
var xmlDoc;
// code for IE
if (window.ActiveXObject)
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation 
&& document.implementation.createDocument)
  alert('Your browser cannot handle this script');

function displayResult()
// code for IE
if (window.ActiveXObject)
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation 
&& document.implementation.createDocument)
  xsltProcessor=new XSLTProcessor();
  resultDocument = xsltProcessor.transformToFragment(xml,document);
<body id="example" onLoad="displayResult()">

Tip: If you don't know how to write JavaScript, you can study our JavaScript tutorial.

Example Explained:

The loadXMLDoc() Function

The loadXMLDoc() function is used to load the XML and XSL files.

It checks what kind of browser the user has and loads the file.

The displayResult() Function

This function is used to display the XML file styled by the XSL file.

  • Load XML and XSL file
  • Test what kind of browser the user has
  • If the user has a browser supporting the ActiveX object:
    • Use the transformNode() method to apply the XSL style sheet to the xml document
    • Set the body of the current document (id="example") to contain the styled xml document
  • If the user has a browser that does not support the ActiveX object:
    • Create a new XSLTProcessor object and import the XSL file to it
    • Use the transformToFragment() method to apply the XSL style sheet to the xml document
    • Set the body of the current document (id="example") to contain the styled xml document

Try it yourself.

prev next

Altova® XMLSpy® - The world's best-selling XML editor!

Whether you're new to XML or already an advanced user, the user-friendly views and powerful entry helpers, wizards, and debuggers in XMLSpy are designed to meet your XML and Web services development needs from start to finish.

  • XML editor
  • Graphical XML Schema / DTD editors
  • XSLT 1.0/2.0 editor, debugger, profiler
  • XQuery editor, debugger, profiler
  • Support for Office Open XML (OOXML)
  • Graphical WSDL editor & SOAP debugger
  • Java, C#, C++ code generation
  • And much more!

Download a fully functional free 30-day trial today!

Shopping Cart
Shopping Cart
$15 Domain Name
Save $20 / year!
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
Home HOME or Top of Page Validate   Validate   W3C-WAI level A conformance icon Printer Friendly  Printer Friendly

W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.