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

XSLT on the Client Side

XSLT - On the Client Side


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


JavaScript Solution

In the previous chapter, we explained how to use XSLT to transform an XML document into XHTML. We did this by adding an XSL stylesheet to the XML file and letting the browser perform the transformation.

Even though this method works well, including a stylesheet reference in the XML file is not always satisfactory (for example, it will not work in browsers that do not recognize XSLT).

A more general approach is to use JavaScript to perform the transformation.

By using JavaScript, we can:

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

Client-side XSLT transformation is sure to become one of the main tasks performed by future browsers, and we will also see its growth in specific browser markets (Braille, auditory browsers, web printers, handheld devices, and so on).


The XML File and the XSL File

Look at this XML document shown in the previous chapter:

Example

<p>Empire Burlesque</p>

Bob Dylan

USA

Columbia

10.90

1985

.

.

View the XML file。

and the accompanying XSL stylesheet:

Example

 

My CD Collection

 

   

     

     

   

   

   

     

     

   

   

 

Title

Artist

View the XSL file。

Note that this XML file does not contain a reference to the XSL file.

**Important:** The above statement means that an XML file can be transformed using multiple different XSL stylesheets.

Note: make sure the XSL file can be opened normally in the browser via a link, as shown below:

Otherwise, the following error may occur: Uncaught TypeError: Failed to execute 'importStylesheet' on 'XSLTProcessor': parameter 1 is not of type 'Node'.


Transforming XML into XHTML in the Browser

This is the source code used to transform an XML file into XHTML on the client side:

Example

<!DOCTYPE html>
<html>
<head>
<script function loadXMLDoc(filename){if(window>

.

ActiveXObject

)

{

xhttp

=

new

ActiveXObject

(

"

Msxml2.XMLHTTP

"

)

;

}

else

{

xhttp

=

new

XMLHttpRequest

(

)

;

}

xhttp

.

open

(

"

GET

"

,

filename

,

false

)

;

try

{

xhttp

.

responseType

=

"

msxml-document

"

}

catch

(

err

)

{

}

//

Helping IE11

xhttp

.

send

(

"

"

)

;

return

xhttp

.

responseXML

;

}

function

displayResult

(

)

{

xml

=

loadXMLDoc

(

"

cdcatalog.xml

"

)

;

xsl

=

loadXMLDoc

(

"

cdcatalog.xsl

"

)

;

//

code for IE

if

(

window

.

ActiveXObject

||

xhttp

.

responseType

==

"

msxml-document

"

)

{

ex

=

xml

.

transformNode

(

xsl

)

;

document

.

getElementById

(

"

example

"

)

.

innerHTML

=

ex

;

}

//

code for Chrome, Firefox, Opera, etc.

else

if

(

document

.

implementation

&&

document

.

implementation

.

createDocument

)

{

xsltProcessor

=

new

XSLTProcessor

(

)

;

xsltProcessor

.

importStylesheet

(

xsl

)

;

resultDocument

=

xsltProcessor

.

transformToFragment

(

xml

,

document

)

;

document

.

getElementById

(

"

example

"

)

.

appendChild

(

resultDocument

)

;

}

}

script

head

<body onload="displayResult()">
<div id="example" />body html>

Try it »

**Tip:** If you do not know how to write JavaScript, please study our JavaScript Tutorial。

Example explanation:

The loadXMLDoc() function

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

It checks the type of browser the user has and loads the files.

The displayResult() function

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

- Use the transformNode() method to apply the XSL stylesheet to the XML document - Set the body of the current document (id="example") to contain the styled XML document - Create a new XSLTProcessor object and import the XSL file - Use the transformToFragment() method to apply the XSL stylesheet to the XML document - Set the body of the current document (id="example") to contain the styled XML document