- Transformation Methodologies
- Transforming Data
- Templates and Parameters
- Transformations and SAX
- Programming Within a Style Sheet
- Summary
- Review Questions
- Programming Exercises
Programming Within a Style Sheet
In this section, we're going to leave the application alone and look at the style sheet itself.
The XSLT 1.0 Recommendation allows developers to create extension functions and elements that perform sophisticated programming. For example, we could duplicate the vote counting programming from Chapter 5 using just extensions built into the style sheet itself. Rather than creating an application to count the votes, we could simply transform the file.
Extensions can be handy because they allow sophisticated programming that can be carried out anywhere an appropriate transformation engine is available, rather than requiring a number of different applications. For example, IBM's developerWorks site (http://www.ibm.com/developerworks/) receives tutorials from authors in XML. A single transformation (spanning several XSLT style sheets) not only converts the single XML document into individual HTML pages for each tutorial panel, it also moves images into their proper folder, creates PDF files (in two sizes) and makes a ZIP file of all the HTML pages and graphics.
Before going any further, check the documentation for your XSL transformation engine to see whether any additional setup is required. For example, Java's transformation engine requires that bsf.jar be part of the CLASSPATH if any language other than Java is used for the extensions. We're going to start with JavaScript, so we'll need not only bsf.jar (which is part of the Java distribution), but also js.jar, downloadable from http://www.mozilla.org/rhino/
Although extension elements and functions are defined within the XSLT 1.0 Recommendation, there is no guidance on how to actually implement them; different vendors may choose different approaches.
Extension Functions
When we talked about XPath in Chapter 9, we discussed XPath functions. These functions provide a way to perform an action on data. For example, Listing 10.22 shows a style sheet that simply outputs the first letter of each vote.
A Fortunate Choice
Fortunately, our candidates have names that start with different letters. We wouldn't get any useful information if Dregraal's opponent were named D'nx'w!
Listing 10.22 Using a Function
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="votes/voter" /> </xsl:template> <xsl:template match="voter"> <xsl:value-of select="substring(vote, 1, 1)"/> </xsl:template> </xsl:stylesheet>
When we request the value of this built-in function, it executes the appropriate operations and outputs any data returned by the function.
We can use this same principle to define and use custom functions, or extension functions.
In this case, we simply want to transform the votes.xml file using a single XSL file, votes.xsl. Update TransformFile.java to look like Listing 10.7, but with the appropriate filenames.
To use the functions, we'll need to create a new namespace for them so that the processor knows they're extensions, and not mistakes. We'll also create a component that not only explicitly lists the functions we'll be creating, but will eventually hold the appropriate code as well. Listing 10.23 shows the basic form, including references to the soon-to-be-built functions.
Listing 10.23 Creating the Functions
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:results="http://www.example.com/results" extension-element-prefixes="results" version="1.0"> <lxslt:component prefix="results" functions="addVote, getResults"> </lxslt:component> <xsl:template match="/"> <xsl:apply-templates select="votes/voter"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="voter"> <xsl:value-of select="results:addVote(string(vote))"/> </xsl:template> </xsl:stylesheet>
Here we've created an overall extension namespace, aliased with lxslt, to house the elements necessary for actually creating the extension elements and functions. The second new namespace, results, differentiates the extension elements and functions and points back to the component via the prefix attribute. We'll also explicitly list the prefix for extension elements so the processor knows how to handle them.
The component element specifies the functions that are defined within (we'll get to that next).
The functions themselves are called just as an XPath function might be called, using data from the document as any necessary arguments. The first, getResults(), takes no argument, but returns a string to be output to the pagein this case, with the election results. The second, addVote(), takes the value of the vote element and passes it to a function.
The functions themselves are shown in Listing 10.24a.
Listing 10.24a Adding the Functions for a Java Implementation
... <lxslt:component prefix="results" functions="addVote, getResults"> <lxslt:script lang="javascript"> var sparkle, dregraal; sparkle = 0; dregraal = 0; function addVote (thisVote) { if (thisVote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } return null; } function getResults(){ return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } </lxslt:script> </lxslt:component> <xsl:template match="/"> <xsl:apply-templates select="votes/voter"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="voter"> <xsl:value-of select="results:addVote(string(vote))"/> </xsl:template> </xsl:stylesheet>
In this case, we're using JavaScript and embedding the code right in the page. Notice that the functions share names with their extension counterparts, so finding the right function to use is simple.
When we call for the value of addVote, the addVote() function adds the appropriate value and returns null, so nothing is output. When the document is complete, the getResults() function, which simply outputs the appropriate value, is called.
In this way, we can perform actions on data that we can explicitly pass as an argument. Extension elements give us even more power.
C++ and Visual Basic .NET
Prior to version 4.0, MSXML did not include support for XSLT extensions at all. MSXML 4.0 supports extension functions (but not extension elements, which are discussed next) via the msxsl:script element. Listing 10.24b shows an example.
Listing 10.24b Implementing XSLT Extensions Using MSXML
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:results="http://www.example.com/results" version="1.0"> <msxsl:script implements-prefix="results" language="JScript"><![CDATA[ var sparkle, dregraal; sparkle = 0; dregraal = 0; function addVote (thisVote) { if (thisVote == "Sparkle") { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } return ''; } function getResults() { return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } ]]></msxsl:script> <xsl:template match="/"> <xsl:apply-templates select="votes/voter"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="voter"> <xsl:value-of select="results:addVote(string(vote))"/> </xsl:template> </xsl:stylesheet>
For documentation on the msxsl:script element, see the MSXML XSLT Reference Guide at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/xsl_ref_overview_1vad.asp.
PHP and Perl
Sablotron can support JavaScript extensions, but it uses a different function namespace and format than that used in the preceding Java example. JavaScript extension functionality in Sablotron is also not built in by default; you must first install the Mozilla JavaScript library. (This is true even if you have Mozilla itself installed; the browser installation lacks the headers required to build against the library.) Then you must build both PHP and the Perl Sablotron wrapper module XML::Sablotron to explicitly link against the Mozilla JavaScript library as well as against Sablotron itself. It's not a process for the faint of heart; again, if you can find prebuilt binaries of Perl or PHP and Sablotron+JavaScript, you'll probably save yourself much frustration by using them.
Listing 10.24c shows a version of the XSL document in Listing 10.24b that will work with Sablotron.
Listing 10.24c Adding the Functions for Sablotron in PHP or Perl
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://www.exslt.org/functions" xmlns:results="http://www.example.com/results" extension-element-prefixes="func" exclude-result-prefixes="results"> <func:script implements-prefix="results" language="javascript"><![CDATA[ var sparkle, dregraal; sparkle = 0; dregraal = 0; function addVote (thisVote) { if (thisVote == "Sparkle") { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } return ''; } function getResults() { return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } ]]> <xsl:fallback> <xsl:text>Javscript extensions not supported</xsl:text> </xsl:fallback> </func:script> <xsl:template match="/"> <xsl:apply-templates select="votes/voter"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="voter"> <xsl:value-of select="results:addVote(string(vote))"/> </xsl:template> </xsl:stylesheet>
The actual transformation is performed as usual.
Extension Elements
In the next section, we'll take a look at how extension elements enable us to use information about the context node, but first let's look at the elements themselves.
In Listing 10.25, we're turning the addVote function into an addVote element.
Listing 10.25 Creating an Extension Element
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:results="http://www.example.com/results" extension-element-prefixes="results" version="1.0"> <lxslt:component prefix="results" functions="getResults" elements="addVote"> <lxslt:script lang="javascript"> var sparkle, dregraal; sparkle = 0; dregraal = 0; function addVote(ctx, elem) { if (elem.getAttribute("enforced") == "no") { return ("Voting restrictions not enforced."); } else { return ("Voting restrictions enforced."); } if (thisVote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } return null; } function getResults(){ return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } </lxslt:script> </lxslt:component> <xsl:template match="/"> <xsl:apply-templates select="votes/voter/vote"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="vote"> <results:addVote enforced="yes"/> </xsl:template> </xsl:stylesheet>
We've made several changes here. First, we've told the component to treat addVote as an element rather than as a function, so we've changed the signature of addVote() to match that of a function linked to an extension element. We'll talk about the parameters in a moment.
The actual function itself has been changed to an extension element. No attributes are required for an extension element, but we're adding one: enforced. This attribute tells the script whether we're enforcing the rule that says only single beings and hosts can vote; symbionts are not allowed to vote.
In the script itself, the extension element is represented by the second parameter, in this case called elem. To get the value of the enforced attribute, we can use the getAttribute() method. This should look familiar; we're calling the same DOM Element getAttribute() method as before, but in JavaScript instead of Java or the other languages we've been looking at. The elem argument represents a plain old DOM Element object.
Finally, we made a slight change to the XPath expressions, for reasons we'll discuss after the next example.
If we were to execute this transformation, the results would look like this:
Voting restrictions enforced.Voting restrictions enforced.Voting restrictions enforced.Voting restrictions enforced.Voting restrictions enforced.Sparkle: 0 Dregraal: 0
For every vote, the addVote element was accessed, so the addVote() function was called. It output the results of the element test, but no votes were actually added, so the tally is inaccurate.
Next, let's look at that tally.
Element Context
Upon first reflection, you may be wondering how we're going to pass in the vote information if there's no parameter for it, as there was when we were creating custom functions. In fact, there is a way to pass this information to the script: the context node.
At any point during the transformation of a document, a single node acts as the context node. When the style sheet asks for text, it's looking for the text of the context node. When it asks for a child element, it's referring to a child of that node.
We'll pass this context node into the extension element's function, as shown in Listing 10.26.
Listing 10.26 Using Context
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:results="http://www.example.com/results" extension-element-prefixes="results" version="1.0"> <lxslt:component prefix="results" functions="getResults" elements="addVote"> <lxslt:script lang="javascript"> var sparkle, dregraal; sparkle = 0; dregraal = 0; function addVote(ctx, elem) { ctxNode = ctx.getContextNode(); vote = ctxNode.getFirstChild().getNodeValue(); if (elem.getAttribute("enforced") == "no") { //Just add votes if (vote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } } else { voter = ctxNode.parentNode; voterStatus = voter.getAttribute("status"); if (voterStatus.equals("primary")) { if (vote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } } } return null; } function getResults(){ return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } </lxslt:script> </lxslt:component> ... <xsl:template match="vote"> <results:addVote enforced="yes"/> </xsl:template> </xsl:stylesheet>
The first parameter of an extension element's function represents the context of the request. We can get the actual context node, vote, using the getContextNode() method. From here, vote is just a simple DOM Node object, so we can get its value by getting the value of the first child, its text node.
From there, we're checking to see whether voting restrictions are being enforced. If they're not, we're simply updating totals, as before. If voting restrictions are being enforced, we'll need to check the voter's status.
Because the context node is just a DOM Node object, we can get its parent, voter, from the parentNode attribute of the object. (This is comparable to getParentNode() in Java.) The voter's status is represented by the status attribute, so again, we can use a traditional DOM method to get it. If the voter is the primary, we count the vote. If not, we do nothing.
Now when we run the transformation, the voting restrictions will be taken into account:
Sparkle: 1 Dregraal: 2
C++ and Visual Basic .NET
MSXML doesn't currently support extension elements.
PHP and Perl
Sablotron doesn't currently support extension elements.
External Classes
All this is well and good, but what if you don't want to do your extensions in JavaScript? Maybe you want to keep them separate and private. In that case, you'll want to use an external class to hold your functions.
An external class is a single class that holds all the functions that extension elements or functions in use may need to execute. For example, Listing 10.27 shows all of our functions converted to a single Java class, VoteSystem.
Listing 10.27 The VoteSystem Functions
import org.apache.xalan.extensions.XSLProcessorContext; import org.w3c.dom.Element; import org.w3c.dom.Node; public class VoteSystem { int sparkle = 0; int dregraal = 0; public String getResults(){ return "Sparkle: "+sparkle+" Dregraal: "+dregraal; } public void addVote( XSLProcessorContext ctx, Element elem) { Node ctxNode = ctx.getContextNode(); String vote = ctxNode.getFirstChild().getNodeValue(); if (elem.getAttribute("enforced").equals("no")) { //Just add votes if (vote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } } else { Element voter = (Element)ctxNode.getParentNode(); String voterStatus = voter.getAttribute("status"); if (voterStatus.equals("primary")) { if (vote.equals("Sparkle")) { sparkle = sparkle + 1; } else { dregraal = dregraal + 1; } } } } }
Note that these are straight conversions from JavaScript. With the exception of a few variable casting and typing issues, the class is identical to what we had. Now we need to tell the style sheet where to find the functions, as shown in Listing 10.28.
Listing 10.28 Using an External Class
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:results="http://www.example.com/results" extension-element-prefixes="results" version="1.0"> <lxslt:component prefix="results" functions="getResults" elements="addVote"> <lxslt:script lang="javaclass" src="VoteSystem"/> </lxslt:component> <xsl:template match="/"> <xsl:apply-templates select="votes/voter/vote"/> <xsl:value-of select="results:getResults()"/> </xsl:template> <xsl:template match="vote"> <results:addVote enforced="yes"/> </xsl:template> </xsl:stylesheet>
In this case, the only thing that changes is where the processor looks for the functions.
PHP
There is currently no way to write XSL extensions in PHP.
Perl
Sablotron doesn't support writing XSL extensions in Perl, but the Perl wrapper for the Xalan project from the Apache group does provide such support. I've chosen not to use it for examples here for various reasons, but if you need support for Perl extension functions, it's your only option at present. You can find more information about Xalan at http://xml.apache.org/xalan-c, and about XML::Xalan, the Perl wrapper for Xalan, at http://search.cpan.org.