Posts

Showing posts from September, 2012

XPath - the keymaster

As you might already know you won't get anywhere without XPath when working with XSLT. As scary as it might sound (at least I thought it sounded scary), it is no more than the pattern you need to learn to find the information you're looking for. Now you might argue that it can be more complicated than that, but we'll keep it simple in this blog post. We'll use the music XML structure from a previous post. Let’s see some examples: <!-- This code will give you a copy of the XML root of the current structure. --> < xsl:template name = " giveMeTheRoot " > < xsl:copy-of select = " // " /> </ xsl:template >   <!-- This template will give you a COPY of the current node. That means that you will also get the subnodes of the current nodes as well. --> < xsl:template name = " giveMeTheCurrentNode " &

XSLT - Recursion

There are a couple of things that make XSLT hard to manage and recursion is indeed one of them. There is just no way of running a traditional for loop. In order to solve this we need either a structure to iterate on, or we can create a template that calls itself with a counter and control number. If you simply need to iterate on an XML structure, you can do a for-each loop like this: < xsl:for-each select = " .//child::title " > <!-- Do you thing --> </ xsl:for-each > However, if you know that you need to iterate on a procedure "i" times, then we need to complicate things a bit. < xsl:template name = " test3 " > < xsl:param name = " counter " /> < xsl:param name = " i " />   <!-- We do our thing... --> < xsl:copy > < xsl:text > Hello

Working with XSLT variables

Working with variables can get you into any kind of delight or trouble. It brings in many scenarios making it tricky in ways you don't experience with other programming languages. First of all you need to remember that xsl variables are STATIC. That means that you cannot change them once they are set. Also you need to make sure that they are in the correct scope. If you create a variable inside a template, it will only be available inside the template. However, you can create GLOBAL variables outside a template as well. Now on to the code examples. This post is using the XML example from an earlier post on XML and XSLT . <!-- The following example creates variables in different ways. -->   <!-- This variable is a simple string, hardcoded inside the variable node. --> < xsl:variable name = " myString " > < xsl:text > Hello Nerd! </ xsl:text > </

Structuring the XML output.

Note: This post will be used as reference for several posts on XSLT. If you just want to do a simple transformation where you do the same thing over and over again when a template hits, you don't really need to call templates. Say you have a long list of records (the music kind) and you want to extract the ones by Michael Jackson, you can simply perform an if-statement inside the template. <!-- Given a structure like this one... --> < recordCollection > < record > < artist > Madonna </ artist > < title > Like a prair </ title > </ record > < record > < artist > Gorillaz </ artist > < title > Gorillaz </ title > </ record > < record > < artist > Madcon </ artist > < title > Beggin </