| Subcribe via RSS

Navigating to a Sibling Node via XPath

January 4th, 2010 | Comments Off | Posted in XSL

So I’m in an XSLT stylesheet, and I’ve navigated my way down to the node level. Now I’m trying to traverse my way down to the next sibling node and get an attribute out of it. Something like the following:

Not the best XML structure in the world, but what do you do when you have inherited code? After a long time of searching and trying out about 20 different possible iterations, I found the best solution to be:

For a quick translation, we are currently at the question node from the XML sample above, looking to step down into the answer node to get the text value. The second XSLT code block’s select statement tells the routine to get the following sibling node ‘answer’ even more specifically the first answer node following the question node. From there, we just reference the text attribute within the node.

Sort by Date in XSLT

November 17th, 2009 | Comments Off | Posted in XSL

Sometimes the simple things are more difficult than they should be. Take sorting XML records by date through an XSLT stylesheet. Nothing is built in to handle this basic function. So when left to our own devices, we have to break up the date into substrings and compare each piece of the puzzle. In the code below, we are sifting through a date formatted like ’11/17/2009′. To get the year, we start with the 7th character then take the next 4. We evaluate our year, our month then the day and this sorts the dates in ascending order. If we wanted to have these in descending, we’d add the order=”descending” parameter to the xsl:sort.

Adding White Space in XSLT Stylesheet

October 20th, 2009 | Comments Off | Posted in XSL
XSLT can be quirky at times. Try inserting a space between your plain text and a element and its highly likely that you’ll just get a big blob of text. Not particularly helpful. We need a way to preserve that white space in our stylesheet. There are two ways we could approach this. The <xsl:text> element preserves the white space between the tags so we could use this to add our space. Also we this element will retain any line brakes we may add as well. We also can accomplish similar means by using the xsl:space attribute wrapped around the text we are looking to preserve.

Passing Parameters into XSLT Document with ASP.NET

July 28th, 2009 | Comments Off | Posted in ASP.NET, XSL

XSL documents do a great job styling XML content and doing it quickly, but its not always easy to define parameters within. Say you have a variable within your ASP.NET page that you need to send over to your XSL stylesheet, what do we do? XslTransform and XsltArgumentList are the key. In the code below, we define a new XslTransform then go about setting our XsltArgumentList. The XsltArgumentList takes an array of parameters so feel free to load it up. Once we have all of our parameters defined, we move to loading up our XSLT document via the Load method within XslTransform. The Server.MapPath just traces our path down the directory structure to the document in question. Finally, we need to associate all our content with the asp:Xml control on the page. We load the XML up. In this case, I had it in a string object. The we transform the content with the Xsl document we defined. Next, we associate our parameters we defined.

Now let’s take a look at the XSLT document. We have to define the xsl:param node so we have a placeholder to move our value into. These parameters definitions rest in the header of the document between the xsl:stylesheet and the xsl:template tags. As you can see, we’ve named our variable ‘myVariable’ and given it a default value of ’123′ in case we don’t have a value in which to send over. This default value will be overwritten when we make our call from the ASPX page. Then all we have to do is call our variable using the xsl:value-of select. Be sure to prepend the variable name with the dollar sign so it can properly access the value.

Working with XSLTransform and ASP:XML Controls in ASP.NET

July 7th, 2009 | Comments Off | Posted in ASP.NET, XML, XSL

Its a bit criminal how long its been since I worked with XSLT. I had this hellish project early in my programming career building a XML/XSLT program in ASP (yeah it was a while ago). I think it scarred me so bad I never had a desire to touch the stuff again. Really since you are able to read XML data straight into GridViews, there isn’t a whole lot of instances in my day to day coding life where I can’t just get by with that. Well today I had an issue where I was getting an XML datafeed from an affiliate provider and trying to find the best way to consume it. The big gotcha that kept me from going the easy GridView route was the format the data was in. It is ugly. The defining values are housed in the attributes and the node names are all the same (see below). What numbnuts designed this thing?

Anyway after trying to traverse the nodes, regenerate the XML document and other generally painful things to get the data to cooperate, I had a eureka moment to just plug it into an XSLT stylesheet. Talk about making my life incredibly easy. Here is the grand total of what it took to get the two elements plugged into my ASPX page. Notice the top 4 lines are my call to the affiliate site to get the XML dataset and line 5 is plugging the XML into the asp:XML control. Basically, one line of code. I like it.

Next, its time to dig into the XSLT. I've stripped this down to the essentials so we can focus on what is going on. Our xsl:for-each statement on line 15 drops down into the /response/result/hot node then our xsl:when tests to insure we bypass any test records the affiliate provider may have included. Next, we loop through the str node and pull out all of our relevant attributes (i.e. str[@name='description']). On line 41, we throw in our catchall for those datasets that come back without records.

All in all its pretty straight forward. I had lots of bells and whistles I had to incorporate to make this makeshift GridView work like it traditionally would under ASP.NET. Over the next few days, I'll post these extras.

Tags: , , , ,