- XSLT - a language for transforming XML documents into XHTML or other xml documents
- XPath - a language for navigating in XML documents
- XSL-FO - a language for formatting XML documents
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. They are completely synonymous.
Here is one sample xml "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>
<cd>
<title>Right Here Waiting for You</title>
<artist>Richard Marx</artist>
<country>USA</country>
<company>Columbia</company>
<price>19.99</price>
<year>1985</year>
</cd>
</catalog>
Here is cdCatalog.xsl
<?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>
Here is the result to view cdCatalog.xml in IE.
The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. match="/" defines the whole document.
The <xsl:value-of> element is used to extract the value of a selected node. The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system, e.g., <xsl:value-of select="catalog/cd/title"/>
The XSL <xsl:for-each> element can be used to iterate every XML element of a specified node-set.
We can also filter the output from the XML file by adding a criterion to the select attribute in the
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
The <xsl:sort> element is used to sort the output. <xsl:sort> is added inside the <xsl:for-each>. For example
<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>
The <xsl:if> element is used to put a conditional test against the content of the XML file. <xsl:if> is added inside the <xsl:for-each> element in the XSL file. For example, the code below will only display the cd which price > 10.
<xsl:for-each select="catalog/cd">
<xsl:if test="price & gt; 10"> <!-- remove the space in & gt; -->
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. It just like "if...else...". For example,
<xsl:choose>
<xsl:when test="price > 10">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:when test="price > 9">
<td bgcolor="#cccccc">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
It will display like this
The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes.
The <xsl:apply-templates> element applies a template to the current element. If we add a select attribute to the
<?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 >
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span >
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
The <xsl:call-template> element calls a named template.
<xsl:template match="car">
<fo:block>
<fo:inline>Car Description</fo:inline>
</fo:block>
</xsl:template>
The xsl below has the same effect.
<xsl:template match="car">
<xsl:call-template name="description"/>
</xsl:template>
<xsl:template name="description">
<fo:block>
<fo:inline>Car Description</fo:inline>
</fo:block>
</xsl:template>
Refer to tutorial for W3C
No comments:
Post a Comment