Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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
Reply With Quote
  #5 (permalink)  
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
Reply With Quote
  #6 (permalink)  
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
Reply With Quote
  #7 (permalink)  
Old July 10th, 2008, 12:21 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #8 (permalink)  
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:38 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.