XSL Tutorial-from w3schools.com

XSL Languages

prev next

It started with XSL and ended up with XSLT, XPath, and XSL-FO.


It Started with XSL

XSL stands for eXtensible Stylesheet Language.

The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML based Stylesheet Language.


CSS - HTML Style Sheets

HTML uses predefined tags and the meanings of tags are well understood.

The <table> element defines a table and a browser knows how to display it.

Adding styles to HTML elements is also simple. Telling a browser to display an element in a special font or color, is easily done with CSS. 


XSL - XML Style Sheets

XML does not use predefined tags (we can use any tags we like) and the meanings of these tags are not well understood.

The <table> could mean an HTML table or a piece of furniture, and a browser does not know how to display it.

There must be something in addition to the XML document that describes how the document should be displayed; and that is XSL!


XSL - More Than a Style Sheet Language

XSL consists of three parts:

  • XSLT is a language for transforming XML documents
  • XPath is a language for defining parts of an XML document
  • XSL-FO is a language for formatting XML documents

Think of XSL as a set of languages that can transform XML into XHTML, filter and sort XML data, define parts of an XML document, format XML data based on the data value, like displaying negative numbers in red, and output XML data to different media, like screens, paper, or voice. 


This Tutorial is About XSLT

The rest of this tutorial is about XSLT - the language for transforming XML documents.

But you can also study our XPath TutorialXSL-FO TutorialW3C XSL activities.


Introduction to XSLT

prev next

XSLT is a language for transforming XML documents into other XML documents.

XPath is a language for defining parts of an XML document.


What You Should Already Know

Before you study XSLT you should have a basic understanding of XML and XML Namespaces.

If you want to study these subjects first, please read our XML Tutorial.


XSLT - XSL Transformations

XSLT is the most important part of the XSL Standards. It is the part of XSL that is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.

XSLT can also add new elements into the output file, or remove elements. It can rearrange and sort elements, and test and make decisions about which elements to display, and a lot more.

A common way to describe the transformation process is to say that XSLT transforms an XML source tree into an XML result tree.


XSLT Uses XPath

XSLT uses XPath to define the matching patterns for transformations.

If you want to study XPath first, please read our XPath Tutorial.


How does it Work?

In the transformation process, XSLT uses XPath to define parts of the source document that match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document. The parts of the source document that do not match a template will end up unmodified in the result document.


XSLT is a Web Standard

XSLT became a W3C Recommendation 16. November 1999.

To read more about the XSLT activities at W3C please read our W3C Tutorial.


XSLT Browsers

prev next

Not all Internet browsers have full support for XSLT.


Internet Explorer 5 is Bad

XSLT in Internet Explorer 5 is NOT compatible with the official W3C XSL Recommendation.

When Internet Explorer 5.0 was released in March 1999, the XSLT standard was still a W3C Working Draft.

Since the final W3C XSL Recommendation is different from the Working Draft, the support for XSL in IE 5 is not 100% compatible with the official XSLT Recommendation.

This restriction applies to both IE 5.0 and IE 5.5.


Internet Explorer 6 is Better

Internet Explorer 6 fully supports the official W3C XSLT Recommendation.

The XML Parser 3.0 - shipped with Internet Explorer 6.0 and Windows XP - is based on both the W3C XSLT 1.0 and the W3C XPath 1.0 Recommendations.

If you are serious about learning XSLT you should upgrade to Internet Explorer 6.0. 


Netscape 6

Netscape 6 isn't fully supporting the official W3C XSLT Recommendation. However, most of the examples in this tutorial will also work in Netscape 6.


Netscape 7

Netscape 7 supports the official W3C XSLT Recommendation.


Internet Explorer MSXML Parser

MSXML Parser 2.0 is the XML parser that is shipped with IE 5.0.

MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.

MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.

According to Microsoft, the MSXML Parser 3.0 is 100% compatible with the official W3C XSLT Recommendation: "MSXML 3.0 offers a significant advancement over MSXML 2.5: server-safe HTTP access, complete implementation of XSLT and XPath, changes to SAX (Simple API for XML), higher conformance with W3C standards, and a number of bug fixes."

For more info: http://msdn.microsoft.com/xml/general/xmlparser.asp

You can read more about the latest releases of IE in our Browser Section.


XSLT - Transformation

prev next

Example study: How to transform XML into XHTML using XSLT.

The details of this example will be explained in the next chapter. 


Correct Style Sheet Declaration

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.

Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!

The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:

<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">

Note: The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" identifies the official W3C XSL recommendation namespace. If you use this namespace, you must also include the attribute version="1.0".

Note: If you are using IE 6.0 or Netscape 6 you should use one of the codes above.


Incorrect Style Sheet Declaration

This was the correct way to declare an XSL style sheet according to the W3C XSL Working Draft:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/TR/WD-xsl">

Note: The declaration above is OUTDATED, but if you are using IE 5 you should use the (incorrect) code above.


Start with your XML Document

We want to transform the following XML document ("cdcatalog.xml") into 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>

To view an XML / XSL document in IE 5.0 (and higher) and Netscape 7 you can click on a link, type the URL in the address bar, or double-click on the name of an XML file in a files folder.

To view an XML / XSL document in Netscape 6 you'll have to open the XML file and then right-click in XML file and select "View Page Source".

View XML file


Create an XSL Style Sheet

Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:

<?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 align="left">Title</th>
      <th align="left">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>

View XSL file


Link the XSL Style Sheet to the XML Document

Finally, add an XSL Style Sheet reference to your XML document ("cdcatalog.xml"):

<?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 have an XSLT compliant browser it will nicely transform your XML into XHTML!

View the result in IE 6 or Netscape 6 and 7

View the result in IE 5


Example Explained

The details of the example above will be explained in the next chapters!


The <xsl:template> Element

prev next

An XSL style sheet consists of a set of rules called templates.

Each <xsl:template> element contains rules to apply when a specified node is matched.


XSLT uses Templates

The <xsl:template> element contains rules to apply when a specified node is matched.

The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

The following XSL Style Sheet contains a template to output the XML CD Catalog from the previous chapter:

<?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>
     <tr>
       <td>.</td>
       <td>.</td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Since the style sheet is an XML document itself, the document begins with an xml declaration: <?xml version="1.0" encoding="ISO-8859-1"?>.

The <xsl:stylesheet> tag defines the start of the style sheet.

The <xsl:template> tag defines the start of a template. The match="/" attribute associates (matches) the template to the root (/) of the XML source document.

The rest of the document contains the template itself, except for the last two lines that defines the end of the template and the end of the style sheet.

The result of the transformation will look (a little disappointing) like this:

My CD Collection

TitleArtist
..

If you have Netscape 6 or IE 5 or higher you can view: the XML filethe XSL file, and the result

The result from this example was a little disappointing, because no data was copied from the XML document to the output.

In the next chapter you will learn how to use the <xsl:value-of> element to select the value of an XML element.


The <xsl:value-of> Element

prev next

The <xsl:value-of> element extracts the value of a selected node.


The <xsl:value-of> Element

The <xsl:value-of> element can be used to select the value of an XML element and add it to the output stream of the transformation:

<?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>
     <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Note: The value of the required select attribute contains an XPath expression. It works like navigating a file system where a forward slash (/) selects subdirectories.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan

If you have Netscape 6 or IE 5 or higher you can view the XML file and the XSL file

View the result in IE 6 or Netscape 6 and 7

View the result in IE 5

The result from this example was also a little disappointing, because only one line of data was copied from the XML document to the output.

In the next chapter you will learn how to use the <xsl:for-each> element to select the values of several XML elements, and add them to the output.


The <xsl:for-each> Element

prev next

The <xsl:for-each> element allows you to do looping in XSLT.


The <xsl:for-each> Element

The XSL <xsl:for-each> element can be used to select every XML element of a specified node set:

<?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>

Note: The value of the required select attribute contains an XPath expression. It works like navigating a file system where a forward slash (/) selects subdirectories.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
Greatest HitsDolly Parton
Still got the bluesGary More
ErosEros Ramazzotti
One night onlyBee Gees
Sylvias MotherDr.Hook
Maggie MayRod Stewart
RomanzaAndrea Bocelli
When a man loves a womanPercy Sledge
Black angelSavage Rose
1999 Grammy NomineesMany
For the good timesKenny Rogers
Big Willie styleWill Smith
Tupelo HoneyVan Morrison
SoulsvilleJorn Hoel
The very best ofCat Stevens
StopSam Brown
Bridge of SpiesT`Pau
Private DancerTina Turner
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
The dock of the bayOtis Redding
Picture bookSimply Red
RedThe Communards
Unchain my heartJoe Cocker

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file

View the result with Netscape 6 or IE 6

View the result with IE 5


Filtering the Output

We can filter the output from an XML file by adding a criterion to the select attribute in the <xsl:for-each> element.

<xsl:for-each select="catalog/cd[artist='Bob Dylan']">

Legal filter operators are:

  • =  (equal)
  • != (not equal)
  • &lt; less than
  • &gt; greater than

Take a look at the adjusted XSL style sheet:

<?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[artist='Bob Dylan']">
   <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>


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

View the result with IE 5


The <xsl:sort> Element

prev next

The <xsl:sort> element is used to sort the output.


Where to put the Sort Information

To output the XML file as an XHTML file, and sort it at the same time, simply add a sort element inside the for-each element in your XSL file:

<?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">
      <xsl:sort select="artist"/>
      <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>

The select attribute indicates what XML element to sort on.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
RomanzaAndrea Bocelli
One night onlyBee Gees
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
The very best ofCat Stevens
Greatest HitsDolly Parton
Sylvias MotherDr.Hook
ErosEros Ramazzotti
Still got the bluesGary Moore
Unchain my heartJoe Cocker
SoulsvilleJorn Hoel
For the good timesKenny Rogers
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
1999 Grammy NomineesMany
The dock of the bayOtis Redding
When a man loves a womanPercy Sledge
Maggie MayRod Stewart
StopSam Brown
Black angelSavage Rose
Picture bookSimply Red
Bridge of SpiesT`Pau
RedThe Communards
Private DancerTina Turner
Tupelo HoneyVan Morrison
Big Willie styleWill Smith

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:sort> element.


The <xsl:if> Element

prev next

The <xsl:if> element contains a template that will be applied only if a specified condition is true.


Where to put the IF condition

To put a conditional if test against the content of the file, simply add an <xsl:if> element to your XSL document like this:

<xsl:if test="price &gt; 10">
  some output ...
</xsl:if>

The value of the required test attribute contains the expression to be evaluated.

Take a look at the adjusted XSL style sheet:

<?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">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The code above only selects the title and artist IF the price of the cd is higher than 10.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan
Still got the bluesGary Moore
One night onlyBee Gees
RomanzaAndrea Bocelli
Black AngelSavage Rose
1999 Grammy NomineesMany

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:if> element.


The <xsl:choose> Element

prev next

The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.


Where to put the Choose Condition

To insert a conditional choose test against the content of the XML file, simply add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to your XSL document like this:

<xsl:choose>
   <xsl:when test="price &gt; 10">
      ... some code ...
   </xsl:when>
   <xsl:otherwise>
      ... some code ....
   </xsl:otherwise>
</xsl:choose>

Look at the adjusted XSL style sheet:

<?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>
      	<xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The code above will add a pink background-color to the artist column WHEN the price of the cd is higher than 10.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
Greatest HitsDolly Parton
Still got the bluesGary Moore
ErosEros Ramazzotti
One night onlyBee Gees
Sylvias MotherDr.Hook
Maggie MayRod Stewart
RomanzaAndrea Bocelli
When a man loves a womanPercy Sledge
Black angelSavage Rose
1999 Grammy NomineesMany
For the good timesKenny Rogers
Big Willie styleWill Smith
Tupelo HoneyVan Morrison
SoulsvilleJorn Hoel
The very best ofCat Stevens
StopSam Brown
Bridge of SpiesT`Pau
Private DancerTina Turner
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
The dock of the bayOtis Redding
Picture bookSimply Red
RedThe Communards
Unchain my heartJoe Cocker

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:choose> element.


Another Example

Here is another example that contains several <xsl:when> elements:

<?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>
      	<xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:when test="price &gt; 9 and price &lt;= 10">
            <td bgcolor="#cccccc">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

The code above will add a pink background color to the artist column WHEN the price of the cd is higher than 10, and a grey background-color WHEN the price of the cd is higher than 9 and lower or equal to 10.


The Result

The result of the transformation will look like this:

My CD Collection

TitleArtist
Empire BurlesqueBob Dylan
Hide your heartBonnie Tyler
Greatest HitsDolly Parton
Still got the bluesGary Moore
ErosEros Ramazzotti
One night onlyBee Gees
Sylvias MotherDr.Hook
Maggie MayRod Stewart
RomanzaAndrea Bocelli
When a man loves a womanPercy Sledge
Black angelSavage Rose
1999 Grammy NomineesMany
For the good timesKenny Rogers
Big Willie styleWill Smith
Tupelo HoneyVan Morrison
SoulsvilleJorn Hoel
The very best ofCat Stevens
StopSam Brown
Bridge of SpiesT`Pau
Private DancerTina Turner
Midt om nattenKim Larsen
Pavarotti Gala ConcertLuciano Pavarotti
The dock of the bayOtis Redding
Picture bookSimply Red
RedThe Communards
Unchain my heartJoe Cocker

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:choose> element.


The <xsl:apply-templates> Element

prev next

The <xsl:apply-templates> element applies a template rule to the current element or to the current element's child nodes.


The <xsl:apply-templates> Element

The <xsl:apply-templates> element applies a template rule to the current element or to the current element's child nodes.

If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed.

Look at the following XSL style sheet:

<?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> 
<xsl:apply-templates/> 
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/> 
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>


The Result

The result of the transformation will look like this:

My CD Collection

Title: Empire Burlesque
Artist: Bob Dylan

Title: Hide your heart
Artist: Bonnie Tyler

Title: Greatest Hits
Artist: Dolly Parton

Title: Still got the blues
Artist: Gary Moore

Title: Eros
Artist: Eros Ramazzotti

Title: One night only
Artist: Bee Gees

Title: Sylvias Mother
Artist: Dr.Hook

Title: Maggie May
Artist: Rod Stewart

Title: Romanza
Artist: Andrea Bocelli

Title: When a man loves a woman
Artist: Percy Sledge

Title: Black angel
Artist: Savage Rose

Title: 1999 Grammy Nominees
Artist: Many

Title: For the good times
Artist: Kenny Rogers

Title: Big Willie style
Artist: Will Smith

Title: Tupelo Honey
Artist: Van Morrison

Title: Soulsville
Artist: Jorn Hoel

Title: The very best of
Artist: Cat Stevens

Title: Stop
Artist: Sam Brown

Title: Bridge of Spies
Artist: T`Pau

Title: Private Dancer
Artist: Tina Turner

Title: Midt om natten
Artist: Kim Larsen

Title: Pavarotti Gala Concert
Artist: Luciano Pavarotti

Title: The dock of the bay
Artist: Otis Redding

Title: Picture book
Artist: Simply Red

Title: Red
Artist: The Communards

Title: Unchain my heart
Artist: Joe Cocker

If you have Netscape 6 or IE 5 or higher you can view: the XML file and the XSL file.

View the result with Netscape 6 or IE 6

Note: Unable to view the result in IE 5, because the "http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:apply-template> element.


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 chapter we explained how XSLT can be used to transform a document from XML to XHTML. We added 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 (i.e. it will not work in a non XSLT aware browser.)

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

By using JavaScript, we can:

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

That's 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

Take a new look at the XML document that you saw in the previous chapters:

<?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>

If you have Netscape 6 or IE 5 or higher you can view the XML file.

And the accompanying XSL style sheet:

<?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 align="left">Title</th> 
        <th align="left">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>

If you have Netscape 6 or IE 5 or higher you can view the XSL file.

Note: Be sure to 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 files.


Transforming XML to XHTML in Your Browser

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

<html>
<body>
<script type="text/javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

</body>
</html>

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

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML document into memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The last line of code transforms the XML document using the XSL document, and writes the result to the XHTML document. Nice!

If you have IE 6.0: See how it works.

If you have IE 5.0: See how it works.


XSLT - On the Server

prev next

Since not all browsers support XSLT, one solution is to transform the XML to XHTML on the server.


A Cross Browser Solution

In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We let a JavaScript use an XML parser to do the transformation. This solution will not work with a browser that doesn't support an XML parser.

To make XML data available to all kinds of browsers, we have to transform the XML document on the SERVER and send it as pure XHTML to the BROWSER.

That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of future browsers.

XSLT transformation on the server is bound to be a major part of the Internet Information Server 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 XSLT file

Take a new look at the XML document that you saw in the previous chapters:

<?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>

If you have Netscape 6 or IE 5 or higher you can view the XML file.

And the accompanying XSL style sheet:

<?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 align="left">Title</th> 
        <th align="left">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>

If you have Netscape 6 or IE 5 or higher you can view the XSL file.

Note: Be sure that the XML file does not have a reference to the XSL file.

IMPORTANT: The above sentence indicates that an XML file on the server could be transformed using many different XSL files.


Transforming XML to XHTML on the Server

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

<%
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("cdcatalog.xml"))

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("cdcatalog.xsl"))

'Transform file
Response.Write(xml.transformNode(xsl))
%>

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

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML file into memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The last line of code transforms the XML document using the XSL document, and returns the result to the browser. Nice!

See how it works.


XML Editors

prev next

If you are serious about XML, you will benefit from using a professional XML Editor.


XML is Text Based

XML is a text based markup language.

One great thing about XML is that XML files can be created and edited using a simple text editor like Notepad.

However, when you start working with XML, you will soon find that it is better to edit XML documents using a professional XML editor.


Why Not Notepad?

Many "hardcore" web developers use Notepad to edit both HTML and XML documents because Notepad is free and simple to use, and personally I often use Notepad for quick editing of simple HTML, CSS, and XML files.

But, if you use Notepad for XML editing, you will soon run into problems.

Notepad does not know that you are writing XML, so it will not be able to assist you. You will create many errors, and as your XML documents grow larger you will lose control.


Why an XML Editor?

Today XML is an important technology, and everyday we can see XML playing a more and more critical role in new web development.

New development projects use XML based technologies like:

  • XML Schema to define XML structures and data types
  • XSLT to transform XML data
  • SOAP to exchange XML data between applications
  • WSDL to describe web services
  • RDF to describe web resources
  • XPath and XQuery to access XML data
  • SMIL to define graphics... and much more

To be able to write XML documents for all your new development projects, you will need an intelligent editor to help you write error free XML documents.


XML Editors

Good XML editors will help you to write error free XML documents, validate your text against a DTD or a schema, and force you to stick to a valid XML structure.

A good XML editor should be able to:

  • Add closing tags to your opening tags automatically
  • Force you to write valid XML
  • Verify your XML against a DTD
  • Verify your XML against a Schema
  • Color code your XML syntax

XMLSPY

At W3Schools we have been using XMLSPY for many years. XMLSPY is our favorite XML editor. These are some of the features we specially like:

  • Easy to use
  • Syntax coloring
  • Automatic tag completion
  • Automatic well-formed check
  • Easy switching between text view and grid view
  • Built in DTD and / or Schema validation
  • Built in graphical XML Schema designer
  • Powerful conversion utilities
  • Database import and export
  • Built in templates for most XML document types
  • Built in XPath analyzer
  • Full SOAP and WSDL capabilities
  • Powerful project management

Read more about XMLSPY


jmeter-results-shanhe-me.xsl是一个用于下载JMeter测试结果的样式表。JMeter是一款流行的性能测试工具,用于模拟多种负载条件下的应用程序性能测试。通过运行JMeter测试脚本,我们可以获得许多有关应用程序性能的指标和数据。但是,这些数据通常以XML格式呈现,不够直观和易读。 这时,我们可以使用jmeter-results-shanhe-me.xsl来将JMeter测试结果转换为更易于阅读和理解的格式。这个样式表可以将XML格式的结果文件转换为HTML或其他可视化格式,以便更方便地分析和解读测试结果。 要使用jmeter-results-shanhe-me.xsl进行下载和使用,您需要将它与JMeter测试结果文件放在同一目录下。然后,您可以通过以下步骤来下载和使用这个样式表: 1. 打开JMeter测试结果文件所在的目录。 2. 右键单击“jmeter-results-shanhe-me.xsl”文件,并选择“另存为”或“下载”选项,以将样式表保存到您的计算机上。 3. 将样式表文件重命名为“jmeter-results.xsl”(确保与测试结果文件命名一致)。 4. 打开JMeter测试结果文件(通常是一个以“.jtl”为扩展名的文件)。 5. 在JMeter的菜单栏中,选择“查看”>“默认样式表”,然后选择“其他样式表”。 6. 在弹出窗口中,选择您之前保存并重命名的“jmeter-results.xsl”文件。 7. 单击“确定”按钮,样式表将被应用于测试结果文件。 8. 现在,您可以通过浏览器或其他支持HTML格式的工具来打开和查看测试结果文件。 通过使用jmeter-results-shanhe-me.xsl样式表,您可以更轻松地查看和分析JMeter测试结果,以便更好地了解应用程序的性能和响应情况。这对于测试工程师和开发人员来说是非常有价值的,可以帮助他们发现潜在的性能问题并进行优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值