[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

Subject: value-of or apply-templates: What is "best"?
From: Kaz <kaz@xxxxxxxxxxxxxxxxxxx>
Date: Wed, 30 May 2007 23:03:32 +0200
 value-of or apply-templates: What is "best"?
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Cast Your Vote

We need your help – Vote for DataDirect XML Products!

  • Best SOA or XML site

Winners and finalists announced at SOA World Conference in November.

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.