[xsl] value-of or apply-templates: What is "best"?

Subject: [xsl] value-of or apply-templates: What is "best"?
From: Kaz <kaz@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 30 May 2007 23:03:32 +0200
Hello all,

I'm having a hard time understanding when and why I should use value-of or apply-templates.

Assuming a simple XML document:

<root_element>
   <head>
       Name of website
   </head>
   <content>
       <headline>
           Interesting headline
       </headline>
       <body>
           Even more interesting body.
       </body>
   </content>
   <footer>
       Copyright not reserved. Use as you please!
   </footer>
</root_element>

I've written two XSL scripts to handle the above. The first one looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


<xsl:output method="xml" encoding="ISO-8859-1" omit-xml-declaration="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

<xsl:template match="root_element">
<html>
<head> <title>
<xsl:value-of select="head"/>
</title>
</head>
<body>
<xsl:apply-templates select="content"/>
<xsl:apply-templates select="footer"/> </body>
</html>
</xsl:template>


<xsl:template match="content">
   <h4>
       <xsl:apply-templates select="headline"/>
   </h4>
   <p>
       <xsl:apply-templates select="body"/>
   </p>
</xsl:template>

<xsl:template match="content/headline">
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="content/body">
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="footer">
<hr />
<p style="font-size: 11px;">
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>



And the second looks like this:



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


<xsl:output method="xml" encoding="ISO-8859-1" omit-xml-declaration="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

<xsl:template match="root_element">
<html>
<head> <title>
<xsl:value-of select="head"/>
</title>
</head>
<body>
<h4>
<xsl:value-of select="content/headline"/>
</h4>
<p>
<xsl:value-of select="content/body"/>
</p>
<hr />
<p style="font-size: 11px;">
<xsl:value-of select="footer"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


It's obvious that the second one is much shorter and perhaps also a bit more "readable", but is it the right way? Is there even a right way?

I feel somewhat lost here. I'm sure there are strengths and weaknesses to both methods - I just can't put my finger on exactly what is good and bad.

I'm looking for a bit of enlightment on the XSL way of doing things. Hope someone can help.

Sincerely,
Thomas

Current Thread