X hits on this document





52 / 142

Transforming XML with XSLT

Manipulating XML data

CSS files, just like the XSLT files above, process the XML input from top to bottom. This technique does not take advantage of the programmatic characteristics of XSLT. The next example does. First of all, the next example takes input, namely a value to sort by. Second, this XSLT file takes advantage of a few function calls such as count, sum and sort. Herein lies an important distinction between CSS and XSLT. CSS is intended for display, only. XSLT can be used to display XML content. It can be used to manipulate content as well.

In this example, calculations are done on our list of pets. First of all, a count of the number of pets is dis- played as well as their average age. Second, the list of pets can be sorted by their name, age, type, or col- or. To see this in action, try the following command: xsltproc -o pets.html --stringparam sortby age pets2html.xsl pets.xml . Different output can be gotten by changing the sortby value to name, color, or type. What happens if an invalid sortby value is passed to the XSLT file? What happens to the output if no --stringparam values are passed? Why?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- pets2html.xsl -->

<xsl:output method="xml" omit-xml-declaration="no" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<!-- get an input parameter and save it to the variable named sortby; use name by default --> <xsl:param name="sortby" select="'name'"/>

<!-- pets --> <xsl:template match="pets">

<html> <head> <title>Pets</title> </head> <body style="margin: 5%">


Document info
Document views518
Page views524
Page last viewedWed Jan 18 04:03:47 UTC 2017