xsl-list
[Top] [All Lists]

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

2007-05-30 14:04:14
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>