XSLT contains much more. It defines 37 elements divided in 3 categories:
In fact, XSLT is a Turing-complete functional programming language. However, the elements introduced so far lay the foundation for all more advanced XSLT processing.
Some other useful XSLT elements are the following:
We conclude with two stylesheet examples that illustrate the above elements. The first one translates an XML bibliography into an XHTML document. The XML bibliography may include XHTML elements in the abstract field of the bibliography items and it uses namespaces to distingush the bibliography application from the XHTML one. Here are a DTD for the bibliography application and a sample XML document containing a short bibliography. The XSLT document follows:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://www.dimi.uniud.it/~francesc/bib" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="yes" media-type="application/xml"/> <xsl:template match="/"> <html> <head> <title>A small bibliography</title> </head> <body> <h1>A small bibliography</h1> <xsl:if test="b:bibliography/b:article"> <h2>Articles</h2> <ol> <xsl:apply-templates select="b:bibliography/b:article"> <xsl:sort select="b:year" order="descending"/> </xsl:apply-templates> </ol> </xsl:if> <xsl:if test="b:bibliography/b:book"> <h2>Books</h2> <ol> <xsl:apply-templates select="b:bibliography/b:book"> <xsl:sort select="b:year" order="descending"/> </xsl:apply-templates> </ol> </xsl:if> </body> </html> </xsl:template> <xsl:template match="b:bibliography/b:article"> <li><a name="{@b:key}"/> <xsl:apply-templates select="b:author"> <xsl:sort select="b:surname"></xsl:sort> </xsl:apply-templates> <b><xsl:apply-templates select="b:title"/></b>. <xsl:apply-templates select="b:journal"/>, <xsl:apply-templates select="b:year"/>. <xsl:if test="b:cite"> References: <xsl:apply-templates select="b:cite"/> </xsl:if> <xsl:if test="b:abstract"> <br></br> Abstract: <xsl:for-each select="b:abstract/node()"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:if> </li> </xsl:template> <xsl:template match="b:bibliography/b:book"> <li><a name="{@b:key}"/> <xsl:apply-templates select="b:author"> <xsl:sort select="b:surname"></xsl:sort> </xsl:apply-templates> <b><xsl:apply-templates select="b:title"/></b>, <xsl:apply-templates select="b:year"/>. <xsl:apply-templates select="b:publisher"/>. <xsl:apply-templates select="@b:isbn"/>. <xsl:if test="b:cite"> References: <xsl:apply-templates select="b:cite"/> </xsl:if> </li> </xsl:template> <xsl:template match="b:author"> <xsl:apply-templates select="b:name"/> <xsl:apply-templates select="b:surname"/> <xsl:if test="position() != last()">, </xsl:if> <xsl:if test="position() = last()">. </xsl:if> </xsl:template> <xsl:template match="b:name"> <xsl:apply-templates/> <xsl:text> </xsl:text> </xsl:template> <xsl:template match="b:surname"> <xsl:apply-templates/> <xsl:text> </xsl:text> </xsl:template> <xsl:template match="b:cite"> <a href="#{@b:item}"> <xsl:apply-templates select="@b:item"/> </a> </xsl:template> </xsl:stylesheet>
Here is the XHTML output document, and here is the XML input document extended with the stylesheet instruction (they should appear the same if your browser supports XSLT).
As another example, this XSLT document converts an XML bibliography in DBLP format into a BibTeX document:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="/"> <xsl:for-each select="dblp/*"> @<xsl:value-of select="name(.)"/>{<xsl:value-of select="@key"/>, author = {<xsl:apply-templates select="author"/>}, <xsl:for-each select="*[name(.) != 'author']"> <xsl:value-of select="name(.)"/> = {<xsl:value-of select="."/>}, </xsl:for-each>} </xsl:for-each> </xsl:template> <xsl:template match="dblp/*/author[position() < last()]"> <xsl:value-of select="."/> and </xsl:template> </xsl:stylesheet>
Notice how we have used the XPath name() function to retrieve the name of an element. Moreover, the output document is not XML, hence the XML declaration has been omitted. If we apply this stylesheet to our short DBLP bibliography we obtain the following BibTeX instance:
@inproceedings{M4M05, author = {M. Franceschet and E. Zimuel}, title = {Modal logic and navigational XPath: an experimental comparison}, booktitle = {Workshop Methods for Modalities}, pages = {156-172}, year = {2005}, url = {http://www.sci.unich.it/~francesc/pubs/m4m05.pdf}, } @article{JLLI05, author = {M. Franceschet and B. ten Cate}, title = {Guarded fragments with constants}, journal = {Journal of Logic, Language and Information}, volume = {14}, number = {3}, pages = {281-288}, year = {2005}, url = {http://www.sci.unich.it/~francesc/pubs/jlli05.pdf}, }