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
<article author="asdf">
<author name="asdf" dob="12/2/1972">

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

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


At 6:28 AM, Blogger Cyrus Crypt said...

Care to share what is XSLT used for? I've seemed it mentioned many times. I only use XML. What do I lose without using XSLT?

At 8:56 PM, Blogger random said...

Hi Cyrus

XSLT is stylesheet for XML. Though do not mistake it for CSS for XML. XSLT can allow you to make say your website in XML and then use it to convert it to actual HTML. The reason can be you can use the same XML and using different XSLT you can easily target multiple output devices. It can be used to say make forms in xml (Infopath) and then convert that forms to actual HTML on the fly. HTML is only example, basically it can be used to pick a XML file and convert into what you desire.


Post a Comment

<< Home

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