Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 10th, 2008, 11:39 AM
Registered User
 
Join Date: Jul 2008
Location: Hillsdale, NJ, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question on Apply Template

As suggested by one of you, I went through the Tutorials on XSLT.

I got confused when I got to the section on <xsl:apply-templates>.

In the example they have for the section:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/">
  <html>
   <body>
    <h2>My CD Collection</h2>
    <xsl:apply-templates/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="cd">
  <p>
  <xsl:apply-templates select="title"/>
  <xsl:apply-templates select="artist"/>
  </p>
 </xsl:template>

 <xsl:template match="title">
  Title: <span style="color:#ff0000">
           <xsl:value-of select="."/>
         </span>
         <br />
 </xsl:template>
 <xsl:template match="artist">
  Artist: <span style="color:#00ff00">
  <xsl:value-of select="."/></span>
  <br />
 </xsl:template>
</xsl:stylesheet>

What makes it print all the Titles and Artists in the XML file without a <xsl:for-each> element?

Also, with only the first occurence of <xsl:template> element, it dumps the contents of the XML file after printing the heading "My CD Collection".

Venki




Venki
 
Old July 10th, 2008, 11:46 AM
Authorized User
 
Join Date: Jul 2008
Location: karaikal, Pondicherry, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hello Vvenk, can you show the XMl file that you for the example and the output from the xslt ? so that i can help you.

Venki
 
Old July 10th, 2008, 11:49 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Processing starts for the root node /, that template creates the html element with a body child element and a h2 child element of the body and inside the body element has an xsl:apply-templates that processes all child nodes of the root node. The single child node of the root node is the catalog element, as the stylesheet contains no matching template the built-in template is used which simply processes the child nodes of the catalog element. Those are the cd elements and that way the template matching the cd elements is applied to each cd element. That template outputs a p element and applies the templates for title and artist.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 10th, 2008, 11:50 AM
Registered User
 
Join Date: Jul 2008
Location: Hillsdale, NJ, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog_apply.xsl"?>

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

Venki
 
Old July 10th, 2008, 11:59 AM
Authorized User
 
Join Date: Jul 2008
Location: karaikal, Pondicherry, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes vvenk, to have exp simple , the line

 <xsl:template match="cd">

in your code makes to print all "title" and "Artist" which is the child of "cd" node.
the line
 <xsl:template match="cd">
looks for all node with the node name "cd" and once it matches the node ,it applies the template.

As martin said they are choosing the entire xml document using <xsl:template match="/"> by the selecting the root element "/"

let me know if you have any further questions.

Venki
 
Old July 10th, 2008, 12:10 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Venki, your explanation is not correct. / selects respectively matches the root node (the document node) which is quite different from the root element selected by /* (or /catalog in case of the sample document).
So don't confuse the root node with the root element.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 10th, 2008, 12:21 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

xsl:apply-templates selects one or more nodes and tosses them into the air, saying "is anyone interested in these?". Around the playing field are lots of template rules, all waiting for nodes to be thrown into the air. When one of them sees a node that it fancies, it catches it and does the action called for, which will often involve selecting some further nodes and throwing them in the air, a process which carries on until the processing is complete.

The beauty of the mechanism lies in the loose coupling: the rule that throws a node in the air doesn't know who will catch it, and the rule that catches it doesn't know who threw it. This makes template rules highly reusable, and highly adaptable to documents with variable or unpredictable structure.

If xsl:apply-templates is written with no select attribute (a common case) then it selects all the children of the current node. If all the template rules do this, the effect is a recursive walk of the entire document tree, each node being processed by the best matching template rule.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 10th, 2008, 12:21 PM
Authorized User
 
Join Date: Jul 2008
Location: karaikal, Pondicherry, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Martin Honnen
 Venki, your explanation is not correct. / selects respectively matches the root node (the document node) which is quite different from the root element selected by /* (or /catalog in case of the sample document).
So don't confuse the root node with the root element.

--
  Martin Honnen
  Microsoft MVP - XML
thank you Martin for correcting me...still some confusion persists between node and element..
to clear this can you give some exp or example in specific to differentiate this two.



Venki




Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about template precedence asc BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 3 December 1st, 2008 08:14 AM
apply-template from included xslt stylesheet chobo XSLT 9 April 16th, 2008 05:25 PM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 05:09 AM
simple XSLT apply-templates question fannus XSLT 7 March 27th, 2007 02:11 AM
Apply Custom template & import data from text file shampabera Excel VBA 2 August 8th, 2005 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.