<$BlogRSDURL$>

Tuesday, December 14, 2004

XML: XSLT and the fun of torturing yourself 

Current flavor of the month ( actually for a few months) is XSLT. And interstingly today only I read a good comment, I do not know where, that "people love XSLT, because they love to torture themselves". Do not know for others, but I think most XSLT developers will understand the reason without it. It is pretty easy to get burnt doing XSLT. It seems so beautiful, but actually your files can end up looking pretty bad.

I have learnt quite a bit of it in the last few days. One problem stumped me for some time. A beginner's issue maybe. I have found a solution, not sure the perfect one.

Suppose you have an XML file you are applying XSLT to and you want to combine attributes from 2 different elements at same level. Something like
<collections>
<article author="asdf">
<author name="asdf" dob="12/2/1972">
</author></article></collections>

and you want end-result at
<author name="asdf" dob="12/2/1972"></author>

The thing is once you get into article using for-each you cannot get to the author nodes(or can you?). So what I have done is make a call-template with parameters being the article and author node collections. This way you can treat both of them at the same level and both of them will be available to you with in the template. Something like
<xsl:call-template name="A">
<xsl:with-param name="articles" select="article">
<xsl:with-param name="authors" select="author">
</xsl:with-param></xsl:with-param></xsl:call-template>

If someone knows I am doing something stupid post back here please!

This page is powered by Blogger. Isn't yours?