My XSLT Toolbox – 5 Favorite XSLT Books

2009-01-08 3 min read Programming Xslt Eddie

I love reading programming books, especially to learn a new programming language. Learning XSLT, I read a large number of books, as there are quite a few available. The quality of the XSLT books struck me as particularly all over the place, some were quite good while others weren’t even worth the time to skim. So I’m throwing together a simple list of my current collection of XSLT references, which happened to be my favorites of the bunch. These books are all geared towards specific audiences… beginners, advanced, etc, so I included their audiences.

  • XSLT – Mastering XML Transformations, Doug Tidwell
    This is my favorite XSLT book. Mr. Tidwell did a great job of combining an introduction to the language, a tutorial on how to write XSLTs, and a reference all into one book. On top of that, I found it to be written in the clearest, most conversational style I’ve found in many a programming book. I find this book covers 90% of my day-to-day needs, and when I forget how something works, this book usually answers my questions. (Plus, hey, you can get the 1.0 version for about $3 used.)
  • XSLT: Programmer’s Reference, Michael Kay
    If Mr. Tidwell’s book covers 90%, this book covers all 100%, and then some. Mr. Kay (who wrote the Saxon processor, if you weren’t aware) presents what amounts to an annotated specification in book form. One of my co-workers calls this book the XSLT dictionary, and I can’t argue with that. This book is probably best for advanced programmers.
  • XSLT and XPATH on the Edge, Jeni Tennison
    Once you’ve got the basics of the language down, you’ve got to use it to write real-world code. I found this book helps to smooth down the rough edges of working with the language. This book requires a mid-level familiarity with the language.
  • XSLT Cookbook, Second Edition, Salvatore Mangano
    I reach for this book whenever I’ve got to do something weird. I use it to find the solution to some odd edge case, or for my “can I do this with XSLT” questions. The book covers everything from faking regular expressions, to set operations on different node-sets, to functional programming with XSLT. I don’t use it often, but it’s like gold when I do. This book is mostly for advanced users.
  • XPath and XPointer, John E. Simpson
    The content in this book is totally covered in each of the other books, and it isn’t really XSLT, because it only covers XPath. But this book is my simple reference to 90% of the XPath questions I have. It is a nice little book that I could live without, but it certainly makes my life easier having it around. I think new users will likely get the most out of this book. (Another book that can be had for about $3 used.)

(For disclosure, I did make the links amazon referrals. I feel kinda weird, but figured why not. I don’t expect any results, but if I got some, it’d go straight to buying a new book.)

Continue reading

My XSLT Toolbox – Recursive XSLT templates

2008-12-28 4 min read Programming Xslt Eddie

Recursion is one of the core concepts in programming. It’s valuable not only as a technique for writing programs, but as a general concept for solving problems. XSLT provides many useful elements such as for-each (and apply-templates), but occasionally you will run into a problem which must be solved with recursion. Let’s take a look at a real-world (no Fibonacci!!) example, where we have to operate on a simple string of numbers separated by commas. We’ll take a step-by-step approach to writing a recursive template.

Let’s say we have the following source document, short and sweet. We want to take each number, and wrap it with an element.

<?xml version="1.0" encoding="UTF-8"?>
<comma>1,2,3,4,5,6,7,88,99,100</comma>

The easy way to do this is to use the EXSLT str:tokenize function, which takes a string and some delimiters and splits the string based on those delimiters. All we do is add the xmlns:str and extension-element-prefixes attributes to our xsl:stylesheet declaration, and then call the str:tokenize function.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0" xmlns:str="http://exslt.org/strings" 
extension-element-prefixes="str">
 
    <xsl:template match="/>
        <xsl:for-each select="str:tokenize( comma, ',')">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:template>
 
</xsl:stylesheet>

The result is, (new-lines added for readability):

<?xml version="1.0"?>
<token>1</token>
<token>2</token>
<token>3</token>
<token>4</token>
<token>5</token>
<token>6</token>
<token>7</token>
<token>88</token>
<token>99</token>
<token>100</token>

Excellent. But let’s say that we don’t have access to the EXSLT functions, and we have to write a template to perform the same thing.

So now we think up a recursive algorithm. Let’s look at a simplified list with three numbers, such as “1,2,3”. First, we print the “1”, the value before the first comma, and then we discard the first comma. At that point, our list will be “2,3” and we repeat, printing the new first value, and discarding the new first comma. Finally, the list becomes only “3”. There is no comma, so we simply print out the rest of the list, “3”. So we will be recursing over the string printing the first number, and then popping off the first number and first comma. This technique will work with a three number list, or a million-number list (though your processor will probably run out of memory before that).

XPath’s “substring-before”, “substring-after”, and “contains” functions are all of the tools that we’ll need to implement our algorithm. “substring-before” lets us obtain the number before the first comma. “substring-after” lets us discard the first number and first comma, and “contains” allows us figure out the last, comma-less case.

Our function starts in the same manner as all recursive functions, dealing with the last case, and then all of the cases before it. The last case will be the comma-less case from our algorithm. So here’s our template skeleton.

Continue reading