- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
Using XSLT to transform XML into another form has a lot of advantages, not the least of which is flexibility. When you want to change the output, you don't have to go back to the code, you simply have to change the XSLT style sheet. But it's easy to mistake this simplicity for shallowness. XSLT can enable you to do much more than simply reformatting text.
Consider a situation in which not all of the information destined for the final output is in the XML file. How can you incorporate it without complex pre- or post-processing? For example, say we have a file of location information:
<places> <place> <city>New York</city> <state>NY</state> <zip>10001</zip> </place> <place> <city>Boston</city> <state>MA</state> <zip>02134</zip> </place> <place> <city>Denver</city> <state>CO</state> <zip>80002</zip> </place> </places>
What if we wanted to display not just the city, state, and ZIP code, but also the temperature? And to make things more difficult, what if we wanted the live temperature at the moment of transformation?
One option is to make a call to another application, such as a Java class, from within the stylesheet.
For example, consider this basic style sheet:
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head><title>Weather</title></head> <body> <table> <th>City</th><th>State</th><th>ZIP code</th> <th>Current Temp</th> <xsl:apply-templates /> </table> </body> </html> </xsl:template> <xsl:template match="place"> <tr> <td><xsl:value-of select="city"/></td> <td><xsl:value-of select="state"/></td> <td><xsl:value-of select="zip"/></td> <td></td> </tr> </xsl:template> </xsl:stylesheet>
This stylesheet creates an HTML page that displays the data, but doesn't yet include the actual temperature. To get the actual temperature, we can create a simple Java class:
package com.informit.chase; public class Weather extends Object { public static String getTemp(String zip){ String returnTemp = ""; //GET TEMPERATURE HERE return returnTemp; } }
I've left out the implementation of the getTemp()
method because it's not crucial to the discussion; the temperature could be coming from a web service, a thermometer, or some other source. The important part is that this method returns the temperature based on the ZIP code. Now we just have to integrate it into the style sheet.
To call a Java class from an XSLT style sheet, we first have to let the processor know where to find the code. We can do that by defining a new namespace:
<?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="com.informit.chase.Weather" exclude-result-prefixes="java"> <xsl:template match="/"> <html> <head><title>Weather</title></head> <body> ...
Here I've defined the java:
namespace to represent the com.informit.chase.Weather
class. (Note that this class must be on the CLASSPATH
when you perform the transformation.) Now I just have to call it from within the stylesheet itself:
... <xsl:template match="place"> <tr> <td><xsl:value-of select="city"/></td> <td><xsl:value-of select="state"/></td> <td><xsl:value-of select="zip"/></td> <xsl:variable name="theZip" select="zip"/> <xsl:variable name="theTemp" select="java:getTemp($theZip)"/> <td><xsl:value-of select="$theTemp"/>></td> </tr> </xsl:template> ...
First I'm creating a simple variable so I can pass the value of the ZIP code to the method. Next I'm creating a variable and populating it with the results of the method. Note that because the java:
namespace represents the class, we just need to note the actual method. Finally, all we need to do is drop the value into the template.
The result is the final page (with spaces added for intelligibility):
<?xml version="1.0" encoding="UTF-8"?> <html> <head><title>Weather</title></head> <body> <table><th>City</th><th>State</th><th>ZIP code</th> <th>Current Temp</th> <tr> <td>New York</td><td>NY</td><td>10001</td><td>45</td> </tr> <tr> <td>Boston</td><td>MA</td><td>02134</td><td>44</td> </tr> <tr> <td>Denver</td><td>CO</td><td>80002</td><td>76</td> </tr> </table> </body> </html>
Extension methods are completely implementation dependent. For example, MSXML doesn't support Java at all. This version covers calling Java from the XSLT engine built in to Java 1.4. Check the documentation for your XSLT engine to find out how to extend it by calling another application.