|Chapter 6 - We Want Results!|
|XSLT For Dummies|
|by Richard Wagner|
|Hungry Minds 2002|
Im sure youve heard the old expression, Theres more than one way to skin a cat. Although that adage may not make cat lovers like me happy (and may send my two cats hiding under the bed), it sure does apply to XSLT. As you grow more and more comfortable with the language, you find out that more than one way exists to generate the result youre seeking. Whats more, for many tasks , there is no definitive right or wrong way to produce a transformed document.
Having said that, a seasoned XSLT developer knows that, in many instances, a specific technique may be more suitable or advantageous than another. Most of the time the decision comes down to a question of ease versus flexibility: Some methods are easier to do while others give you more power and flexibility.
Consider the following simple example. You can create a new element by using a literal text string within a template rule:
But you can also get identical results by using the xsl:element instruction:
Given that, why does XSLT have two or more methods that produce the same result? And which is the preferred way? For most purposes, the literal text approach is perfectly acceptable and is certainly the easiest to read and write. In contrast, the xsl:element instruction gives you more flexibility by allowing you to name an element, not just from a text string, but also to base it on the result of an XPath expression. Like everything else in life, knowing which technique is the most suitable simply comes with experience.