tags:

views:

762

answers:

11

Recently, I have started to have to read a lot of XSL and XSLT at my job. Some of it makes sense and some of it really doesn't.

Here's an exmaple of what does not make sense to me

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
        <xsl:output method="html" encoding="UTF-8" />
        <xsl:param name="message"/>
        <xsl:template match="/">
        <html><body> header
        <xsl:value-of select="//message"/>
        footer
        </body></html>
        </xsl:template>
</xsl:stylesheet>

If I have an XML file that looks like

<message><message_text>hello, world!</message_text></message>

It appears to result in

<html><body>header 
    <message><message_text>hello, world!</message_text></message>
footer
</body></html>

My questions are like: what does template match="/" do? What does the param name="message"/> do?

Are there any good resources available that contain a quick overview and maybe some examples?

+8  A: 

W3Schools has a very good tutorial with examples here:

http://www.w3schools.com/xsl/

The creator of TopXML has written an article about XSLT and XPath (you need to know XPath to work with XSL) here:

http://www.topxml.com/xsl/tutorials/intro/default.asp


EDIT: For a reference of what the keywords does, W3Schools has made this overview:

http://www.w3schools.com/XSL/xsl_w3celementref.asp

There you can find template match and all the others.

Espo
+2  A: 

zvon.org has got quite a comprehensive tutorial on XSLT as well: http://www.zvon.org/xxl/XSLTutorial/Output/index.html

To answer some of your more specific questions (like "what does xsl:param do?") they also have a good reference section which provides many examples along with the relevant sections from the specification: http://www.zvon.org/xxl/XSLTreference/Output/index.html

BirgerH
+1  A: 

Chapter 8 of XML in a Nutshell has a pretty good introduction to XSLT. I think the next chapter covers XPath too.

Oracle's JDeveloper has a visual XSLT tool that lets you drag and drop from a source schema to a destination schema. This might help you pick up the basics, but it might not be flexible enough once you really get started.

I found it hard to get started using XSL because of the way the file is parsed. Remember, the order that the rules appear is really important.

Matt
+10  A: 

In my opinion the definitive guide to XSLT and XPATH is Michael Kay's Progammers Reference from WROX: http://www.amazon.co.uk/XSLT-XPath-Programmers-Reference-Programmer/dp/0470192747/. I am bias as I technically edited this book.

The newest version of this book does focus on XSLT 2.0 and XPATH 2.0 so some XSLT 1.0 topics aren't covered in as much detail, but it does still list all XSLT 1.0 instructions and functions, so is still a very good reference book.

W3Schools is a great site for an introduction: http://www.w3schools.com/xsl/

In your example above I have the following notes:

  • The <xsl:param> does nothing - as the variable it creates is never used. To use a parameter passed to an XSLT stylesheet you refer to it as a variable $message.
  • <xsl:value-of select="//message"/> searches for all elements called 'message' and then returns the textual value of this. The textual value of a number of elements is basically the concatenation of their text contents, which in this example is hello world!, so what you say it is returning isn't correct.
  • <xsl:template match="/"> is the most complicated to explain easily, but basically it matches the root element of your input XML and processes its contents.
samjudson
ddaa
+1  A: 

I agree with @samjudson (and voted it up). I used Michal Kays XSLT book myself to get up to speed.

One of the biggest points I learned from his book is a good understanding of the inherent declarative, template based approach of XSLT.

Understanding this allows you to write much more clear and maintainable XSLT, when compared to a more procedural (ie conditionals, loops etc) approach often used by newcomers to the topic.

Conditional constructs have their place in XSLT, however knowing when to them versus a more template oriented approach is the tricky part.

Chapter 9 "Stylesheet Design Patterns" describes this very well.

Ash
A: 

After learning the basics you might want to take a look at http://www.dpawson.co.uk/xsl/sect2/sect21.html. It is a XSLT FAQ page answering questions like "How do I test for an empty string?" and "How do I simulate a while loop?".

Leo Lännenmäki
+1  A: 

While it isn't introductory and takes a little effort to wrap your head around, I find the official XSLT specification (along with the XPath specification) a very good online resource. Once you have understood the basic concepts of XSLT (by reading the resources others have posted here), I have found that the specification answers the advanced questions in a concise way.

Christian Berg
+1  A: 

XSLT is difficult to get a grasp on, since a well-written transform can seem "backwards" to a programmer used to procedural programming. You should make an attempt to write "push" transforms instead of "pull" transforms. Here's a good article on that.

Once you get past the basics, the best book, bar none, is Jeni Tennison's XSLT and XPath on the Edge. It goes over things like grouping and managing multi-stylesheet applications. It's written for XSLT 1.0, but still very relevant for XSLT 2.0.

James Sulak
+1  A: 

Jenni Tennison is a member here and has a great set of tutorials.

David Robbins
A: 

For a succinct summary of XSLT for the beginner, there's no better tutorial than Evan Lenz's XSLT 1.0 Pocket Reference. I picked it up when I started learning XSLT a few years ago and it proved to be the perfect tutorial. The first three chapters are the best summary of how XSL works I've ever encountered. It contains exactly the right level of detail for an experienced programmer coming up to speed on XSL. Granted, it covers only XSL 1.0, but after you've digested it you'll be ready for the best reference book, Michael Kay's new XSLT 2.0 and XPath 2.0 Programmer's Reference. I also refer to the specs at w3c.org when I have a very specific question.

The single most important concept to master is that XSL is declarative, not procedural. Let go of the idea that the stylesheet drives the process and embrace the somewhat zen idea that the input XML is in control, and your stylesheet really contains event handlers, not procedural code.

Jim Garrison