Printer Friendly
The Free Library
22,719,120 articles and books

XSLT--basic.



What Is XSLT (eXtensible Stylesheet Language Transformation) Software that converts an XML document into another format such as HTML, PDF or text. It may also be used to convert one XML document to another XML document with a different set of XML tags (different schema). ?

Extensible Stylesheet Language Transformations (World-Wide Web) Extensible Stylesheet Language Transformations - (XSLT) A W3C standard for transforming XML documents into other XML documents or other formats. This was conceived as part of XSL but has been found to have wider applications.

http://w3.org/TR/xslt.
 or XSLT is a language that allows you to transform XML XML
 in full Extensible Markup Language.

Markup language developed to be a simplified and more structural version of SGML. It incorporates features of HTML (e.g., hypertext linking), but is designed to overcome some of HTML's limitations.
 documents into XML, HTML HTML
 in full HyperText Markup Language

Markup language derived from SGML that is used to prepare hypertext documents. Relatively easy for nonprogrammers to master, HTML is the language used for documents on the World Wide Web.
, XHTML (EXtensible HTML) A markup language for Web pages from the W3C. XHTML combines HTML and XML into a single format (HTML 4.0 and XML 1.0). Like XML, XHTML can be extended with proprietary tags. Also like XML, XHTML must be coded more rigorously than HTML. , or plain text documents. It relies on a companion technology called XPath. XPath helps XSLT identify and find nodes in XML documents; nodes are things like dements, attributes, and other objects in XML. With XSLT and XPath, you can do things like transform an XML document into HTML or XHTML so it will easily display in a web browser The program that serves as your front end to the Web on the Internet. In order to view a site, you type its address (URL) into the browser's Location field; for example, www.computerlanguage.com, and the home page of that site is downloaded to you. ; convert from one XML markup vocabulary to another, such as from Docbook to XHTML (see www.docbook.org); extract plain text out of an XML document for use in some other application, like a text editor; or build a new Spanish language Spanish language, member of the Romance group of the Italic subfamily of the Indo-European family of languages (see Romance languages). The official language of Spain and 19 Latin American nations, Spanish is spoken as a first language by about 330 million persons  document by pulling and repurposing all the Spanish text from a multilingual XML document. This is only a start of what you can do with XSLT.

How Does XSLT Work?

The quickest way to get you acquainted with how XSLT works is through a simple example. Consider this ridiculously brief XML document contained in a file I'll call msgxml:

<msg/>

There isn't much to this document, but it's legal, well-formed XML XML data that conforms to the rules of XML syntax. There is typically little or no tolerance for straying from any electronic standard. However, HTML, the tag-based format language created prior to XML, was allowed to meander. : just a single, empty element tag empty element tag - tag  with no content (that is, nothing between a pair of tags). For our purposes, its the source document for the XSLT processing we'll do in a minute. Now you can use the very simple XSLT stylesheet msg.xsl to transform msg.xml:
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform
<output method="text"/>

<template match="msg">Found it!/Transform

</stylesheet>


You'll notice that XSLT is written in XML. This allows you to use some of the same tools to process XSLT stylesheets that you would use to process other XML documents. The first element (start tag start tag - tag , really) in msgxsl is
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform


This is the document element for stylesheet, one of two possible document elements in XSLT. The other possible document element is transform which is actually just a synonym for stylesheet. You can use one or the other. The version attribute in stylesheet is required, along with its value of 1.0. (We're only dealing with version 1.0 of XSLT here.)

The attribute xmlns on stylesheet is a special attribute for declaring a namespace A collection of names for a particular purpose. Typically, each name is unique. For example, tables in a relational database must all have unique names. A Windows workgroup that uses the original NetBIOS naming system requires a different "made-up" name for each computer and printer in . It's value is http://www.w3.org/999/XSL/Transform, which is the official namespace for XSLT. An XSLT stylesheet must always have such a namespace declaration in order for it to work. (XSLT stylesheets usually use the xsl pre fix, as in xsl.:stylesheet, but I am setting the prefix aside for simplicity at the moment. You'll want to use xsl when your stylesheets get only slightly more complex.)

The stylesheet element is followed by the output element which is optional. The value text for the method attribute signals that you want the output of the stylesheet to just be plain text:

<output method="text"/>

Two other possible values for method in XSLT 1.0 are xml and html. (The output element actually has ten attributes, all of which are optional.)

The next element in msgxsl is the template element. This element is at the heart of what XSLT does. A template rule consists of two parts: a pattern, such as an XML element in the source document that you're trying to match, and a sequence of instructions. The match attribute of template contains a pattern, a location path in XPath. The pattern in this example is the name of the rnsg element:
<template match="msg">Found it!</
template>


XPath syntax always appears in attribute values, as in the value of match. The sequence of instructions (sometimes called a sequence constructor) contains only the literal text 'Found it!' Sequence instructions tells an XSLT processor what you want to have happen when the pattern is found in the source. Using this stylesheet, when msg is found in the source by an XSLT processor, it will output the text 'Found it!'. When a template executes its instructions, that template is said to be instantiated. To make this happen, you need an XSLT processor.

How Do I Get XSLT to Work?

An XSLT processor processes a source document with an XSLT stylesheet, producing an output or result. There are lots of free XSLT processors available for download on the web. I'll mention a couple.

Instant Saxon

Michael Kay's free Instant Saxon (saxon.exe) runs on the Windows command line A function within Windows that executes individual commands typed in one at a time or one after the other if they are grouped in a text file with a .CMD or .BAT extension. Widely used by developers and power users for file management and testing, many commands are identical to the . Download it "Download It" is Clea's debut single. It was released in the UK on September 22, 2003 and missed the top 20 charting at #21. The single had average promotion, being performed in shows like Top of the Pops.  from prdownloads-sourceforge.net/saxon/instant_saxon6_5_3.zip. (If the link fails, just try saxon.sourceforg.net. Unzip To decompress a file in the .ZIP file format. See Zip file.

1. (tool, compression) unzip - To extract files from an archive created with PKWare's PKZIP archiver.
2.
 the file in some directory on your Windows box See box. . Assuming that you have created and saved the files msgxml and msg.xsl discussed earlier in the same spot that you unzipped saxon.exe, you can run Instant Saxon from the Windows command line like this:

saxon msg.xml msg.xsl

This command will process msgxml against the stylesheet msgxsl and produce the simple result:

'Found it!'

xRay2

If you prefer a graphical application, Architag offers a free, graphical XML editor An XML editor is a markup language editor with added functionality to facilitate the editing of XML. This could be done in plain text in a text editor, with all the code visible.  with XSLT processing capability called xRay2. It is available for download from www.architag.com/xray. Like Instant Saxon, xRay2 runs only on the Windows platform. Assuming that you have successfully downloaded and installed it, launch xRay2 and open the file msgxml and then open the file msg.xsl. Now select New XSLT Transform from the File menu. In the XML Document pull-down menu Also called a "drop-down menu" or "pop-down menu," the common type of menu used with a graphical user interface (GUI). Clicking a menu title causes the menu items to appear to drop down from that position and be displayed. , select msg.xml, and in the XSLT Program pull-down menu, select msg.xsl (if it is not already checked, check Auto-update). The result of the transformation should appear in the transform window of the application.

Xalan

If you are using the Linux operating system operating system (OS)

Software that controls the operation of a computer, directs the input and output of data, keeps track of files, and controls the processing of computer programs.
 or Some Other Unix flavor, you can run Apache's XSLT processor Xalan C++ (works on Windows, too). In order to run Xalan, you also need the C++ version of Xerces, Apache's XML parser. You can find both Xalan C++ and Xerces C++ on xml.apache.org. After downloading and installing them (follow instructions on the Apache site), you need to make sure that Xalan and Xerces are in your execution path. Now type the following line in a Unix shell window or at a Windows command prompt:

xalan msg.xml msg.xsl

If successful, the following results should be printed on your screen:

'Found it!'

How Do I Get XSLT to Work in a Browser?

An XSLT processor is probably readily available to you on your computer desktop in the form of a web browser: Microsoft Internet Explorer See Internet Explorer.  (IE) Version 6, Netscape Navigator (Netscape) Version 7.1, Mozilla Version 1.4, or Mozilla Firebird 0.7.

Each of these browsers has client-side XSLT processing ability already built into them.

The way to apply an XSLT stylesheet like msg.xsl to the document msg.xml in a browser is by using a processing instruction. A processing instruction (PI) allows you to include instructions for an application in an XML document.

You can see a processing instruction in a slightly altered version of msg.xml, which I call msg-pi.xm:
<?xml-stylesheet href="msg.xsl" type="text/xsl"?>
<msg/>


The XML stylesheet PI should always come before the first element in the document (the document clement msg in msg-pi.xml. The purpose of this PI is similar to one of the purposes of the link tag in HTML, that is, to associate a stylesheet with the document. Save msg-pi.xml in a text file with the other files.

If you open msg-pi.xml in one of the browsers I mentioned, the built-in XSLT processor in the browser will write the string 'Found it!' on the browser's canvas or rendering space

Beware the Built-in Templates

XSLT has a hobgoblin hobgoblin: see goblin.  of sorts. It's a feature (jargon) it's a feature - From the adage "It's not a bug, it's a feature." Used sarcastically to describe an unpleasant experience that you wish to gloss over.  know as built-in templates. Built-in templates automatically find nodes that are not specifically matched by a template rule, so you can sometimes get results from an XSLT stylesheet that you're not expecting. These built-in templates automatically find text (among other things) in the XML source when no explicit template matches that text. This can rattle your nerves at first, but you'll get comfortable with them soon enough. I'll illustrate an instance where the built-in template matches text in an XML document. The file hobgoblin.xml contains a bit of text in the clement msg..

<msg>Spooky!</msg>

To trigger the built-in template for text, the dull-witted stylesheet hobgoblin.xsl will do the trick:
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/
Transform">
<output method="text"/>
</stylesheet>


Apply hobgoblin.xsl to hobgoblin.xml with Instant Saxon using this command..

saxon hobgoblin.xml hobgoblin.xsl

And you will get the following result:

Spooky!

Even though hobgoblin.xsl does not contain a template rule, Instant Saxon found the text Spooky! in the msg element by default using a built-in template rule.

Summary

That covers five basics of XSLT 1.0. This article is only a starting point to get you rolling. There is much, much more to learn about XSLT. Of course, Learning XSLT can help you out there. For resources and news for XSLT from the W3C (World Wide Web Consortium, www.w3.org) An international industry consortium founded in 1994 by Tim Berners-Lee to develop standards for the Web. It is hosted in the U.S. by the Computer Science and Artificial Intelligence Laboratory (CSAIL) at MIT (www.csail.mit.edu/index.php). , go to www.w3.ore/Style. If you're brave enough to read the specs, go to www.w3.org/TRxslt and www.w3.org/TR/xpath to learn more about XSLT 1.0 From Learning XSLT.
COPYRIGHT 2004 A.P. Publications Ltd.
No portion of this article can be reproduced without the express written permission from the copyright holder.
Copyright 2004, Gale Group. All rights reserved. Gale Group is a Thomson Corporation Company.

 Reader Opinion

Title:

Comment:



 

Article Details
Printer friendly Cite/link Email Feedback
Title Annotation:Technote
Publication:Database and Network Journal
Date:Feb 1, 2004
Words:1561
Previous Article:Customer insight cannot be outsourced.
Next Article:Successful identity management.



Related Articles
NIST HOSTS XSL CONFORMANCE TESTING MEETING.
Seque Enhances Silk Support.
W3C and NIST release DOM Conformance Test Suite. (News Briefs).
"Python & XML". (Book Browser).
DataPower and Contivo offer complete standards-based XML integration solution.
Systems integration. (Technology Tools).
Support goes global: from sophisticated Web-based systems to that old standby, the phone, EDA companies have entered a new era of customer support.
Anatomy of an XML document.
XML made easy ... and free.
FREE SPECIAL DEVELOPMENT LICENSES FOR FSA MEMBERS.

Terms of use | Copyright © 2014 Farlex, Inc. | Feedback | For webmasters