|
|
 |
| 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.
|
 |

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
|
|
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
|

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
|
|
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
|

July 10th, 2008, 12:49 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
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
|

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
|
|
<?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
|

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
|
|
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
|

July 10th, 2008, 01:10 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 655
Thanks: 0
Thanked 98 Times in 97 Posts
|
|
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
|

July 10th, 2008, 01:21 PM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

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
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |