XML DOES NOT USE predefined tags and the meanings of these tags are not well understood. The <table> could mean an HTML table or a piece of furniture. Abrowser 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! 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. 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 ! <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used! <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 1 We want to transform the following XML document ("cdcatalog.xml") into XHTML: 2 Create an XSL Style Sheet Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template: 3 Link the XSL Style Sheet to the XML Document Finally, add an XSL Style Sheet reference to your XML document ("cdcatalog-transformed.xml"): 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. <td><xsl:value-of select="catalog/cd/title"/></td> The value of the required select attribute contains an XPath expression. BASICS Loop through all titles: <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> Example filter: <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> Example SORT: ... <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> Example IF: <xsl:for-each select="catalog/cd"> <xsl:if test="price Ampgt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> Example CHOOSE: <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. <xsl:for-each select="catalog/cd"< <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 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>
The XSLT elements from the W3C Recommendation (XSLT Version 1.0).
The links in the "Element" columns point to attributes and more useful information about the specific element.
Note: All elements supported in IE 5.X may have NON-standard behavior, because IE 5.X was released before XSLT became a W3C Recommendation!
Element | Description | IE | NN |
---|---|---|---|
apply-imports | Applies a template rule from an imported style sheet | 6.0 | |
apply-templates | Applies a template rule to the current element or to the current element's child nodes | 5.0 | 6.0 |
attribute | Adds an attribute | 5.0 | 6.0 |
attribute-set | Defines a named set of attributes | 6.0 | 6.0 |
call-template | Calls a named template | 6.0 | 6.0 |
choose | Used in conjunction with <when> and <otherwise> to express multiple conditional tests | 5.0 | 6.0 |
comment | Creates a comment node in the result tree | 5.0 | 6.0 |
copy | Creates a copy of the current node (without child nodes and attributes) | 5.0 | 6.0 |
copy-of | Creates a copy of the current node (with child nodes and attributes) | 6.0 | 6.0 |
decimal-format | Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function | 6.0 | |
element | Creates an element node in the output document | 5.0 | 6.0 |
fallback | Specifies an alternate code to run if the processor does not support an XSLT element | 6.0 | |
for-each | Loops through each node in a specified node set | 5.0 | 6.0 |
if | Contains a template that will be applied only if a specified condition is true | 5.0 | 6.0 |
import | Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet | 6.0 | 6.0 |
include | Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as the including style sheet | 6.0 | 6.0 |
key | Declares a named key that can be used in the style sheet with the key() function | 6.0 | 6.0 |
message | Writes a message to the output (used to report errors) | 6.0 | 6.0 |
namespace-alias | Replaces a namespace in the style sheet to a different namespace in the output | 6.0 | |
number | Determines the integer position of the current node and formats a number | 6.0 | 6.0 |
otherwise | Specifies a default action for the <choose> element | 5.0 | 6.0 |
output | Defines the format of the output document | 6.0 | 6.0 |
param | Declares a local or global parameter | 6.0 | 6.0 |
preserve-space | Defines the elements for which white space should be preserved | 6.0 | 6.0 |
processing-instruction | Writes a processing instruction to the output | 5.0 | 6.0 |
sort | Sorts the output | 6.0 | 6.0 |
strip-space | Defines the elements for which white space should be removed | 6.0 | 6.0 |
stylesheet | Defines the root element of a style sheet | 5.0 | 6.0 |
template | Rules to apply when a specified node is matched | 5.0 | 6.0 |
text | Writes literal text to the output | 5.0 | 6.0 |
transform | Defines the root element of a style sheet | 6.0 | 6.0 |
value-of | Extracts the value of a selected node | 5.0 | 6.0 |
variable | Declares a local or global variable | 6.0 | 6.0 |
when | Specifies an action for the <choose> element | 5.0 | 6.0 |
with-param | Defines the value of a parameter to be passed into a template | 6.0 | 6.0 |