Posts

Showing posts from 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 </

How to create an XSLT template

This is the second post in my flash post series on XSLT. We'll start off with some simple examples and move up to advanced as we get the most important components in place. 1: < -- The most basic thing you should know about is how to create and call a template. 2: A template is a procedure that is executed when a certain criteria is met. 3: That we will come back to. 4: --> 5:   6: < !-- First off is the template. A template is the most basic element in an XSLT sheet. 7: This perticular template targets the XML root node, but gives no output.--> 8:   9: < xsl:template match ="/"> 10: < / xsl:template > 11:   12: < !-- The following template targets any node and attribute, 13: but gives no output.--> 14:   15: < xsl:template match ="@* | node()"> 16: < / xsl:template > 17: 18: < !--Now, for the first two, they are exe

XSLT transformations with .Net

How the transformation works with .Net. Using .Net and C# you can do XSL transformations using XslCompiledTransform(). As input to the XSLT engine we use an XSLT sheet, settings (optional), parameters (optional), an XML source and the output path. Note: Please note that if you need to use XSLT 2.0 or XPath 2.0 you should use a third party XSLT engine. 1: // C# code - using .Net 4.0 2:   3: // We want to enable document() functions and script() inside the XSLT code.  In order to allow this in using .Net we include this in the settings.  This gives us a bit more power as far as .Net XSLT support goes. 4: var settings = new XsltSettings( true , true ); 5:   6: // This is the XSLT engine object 7: var xslTransform = new XslCompiledTransform(); 8:   9: // We load the XSLT and settings to the engine. 10: xslTransform.Load(XSLTSheetPath, settings, new XmlUrlResolver()); 11:   12: // As there is no way of g

Future entries...

Hi there! Long time - no blogging. That is no good. I've realized that if I am to have a blogg, I might as well write from time to time. (However, don't shoot me if I doze off again ;) I'll be posting a series of flash posts - this time about XSLT. My plan is to cover the basics and then move up to more advanced as we go along. Each post will handle small use cases and have a common theme for you to keep up. So, feel free to drop by in the coming weeks. I'll make an effort of keeping my promise this time ;) - Morten