p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > XML > XSLT
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 10th, 2008, 12:39 PM
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
__________________
Venki
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old July 10th, 2008, 12:46 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old July 10th, 2008, 12:49 PM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old July 10th, 2008, 12:50 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old July 10th, 2008, 12:59 PM
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old July 10th, 2008, 01:10 PM
Friend of Wrox
Points: 3,131, Level: 23
Points: 3,131, Level: 23 Points: 3,131, Level: 23 Points: 3,131, Level: 23
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #7 (permalink)  
Old July 10th, 2008, 01:21 PM
mhkay's Avatar
Wrox Author
Points: 12,735, Level: 48
Points: 12,735, Level: 48 Points: 12,735, Level: 48 Points: 12,735, Level: 48
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #8 (permalink)  
Old July 10th, 2008, 01: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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 06:25 PM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 06:09 AM
simple XSLT apply-templates question fannus XSLT 7 March 27th, 2007 03:11 AM
Apply Custom template & import data from text file shampabera Excel VBA 2 August 8th, 2005 08:41 AM



All times are GMT -4. The time now is 02:22 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc