Posts

Showing posts with the label xml xslt xpath

Using parameters in XSLT

If you've read some of my previous posts, you've seen examples of using parameters when calling templates. I earlier wrote about assigning XML structures to variables. Now we are going to use a variable as parameter for a template call. Create the variable and send it as a parameter to your template of choice: <!-- Given a structure like this one... --> <xsl:variable name="someVariable"> <record> <artist sex="female">Madonna</artist> <title>Like a prair</title> </record> </xsl:variable> <xsl:call-template name="targetTemplate"> <xsl:with-param name="string" select="$someVariable" /> <xsl:with-param name="attributeString" select="$someVariable/@sex"/> </xsl:call-template name="targetTemplate"> </xsl:template> Grab the variable and do something that makes sense. ;...

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 > </ ...

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...