Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 October 21st, 2008, 11:00 PM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default difference between these three formats

Hi
the output of these 3 templates are same,whats the difference and in what situations we will use

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

 
Old October 21st, 2008, 11:43 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

First template matches the root element.
Second one matches the text nodes.
Third one matches all other elements. For example
<xsl:template match="date">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="test">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*">
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
The above template will match all elements except date and test elements.

------
Rummy
 
Old October 22nd, 2008, 01:06 AM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

To summarize

/ matches the root node

* matches any element

text() matches text nodes

is my understanding correct?

 
Old October 22nd, 2008, 01:23 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Default

correct

------
Rummy
 
Old October 22nd, 2008, 02:03 AM
Authorized User
 
Join Date: Sep 2008
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default

we have explicit and implicit template rules when will this come into picture

i mean when they will act? i am little bit confused in this concept.

for example when we are using apply-templates

<xsl:apply-templates select="publisher"/>

and we have say

<xsl:template match="publisher/book">
              <xsl:value-of select="author"/>
</xsl:template>

here there is no direct correspondence between the value of the xsl:apply-templates element's select attribute and the match attribute of an xsl:template

at this point of time what will be the output.

Raj








 
Old October 22nd, 2008, 03:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Think of it this way. I'm an apply-templates instruction, and I select a set of nodes. Think of the nodes like tennis balls. I throw the selected balls across a high wall. On the other side of the wall are a set of template rules, waiting for balls to be thrown across. Each template rule is designed to handle different shapes or colours of tennis ball. When a ball comes across, every template rule looks at it and decides "is this one for me?". If it is, then it tries to catch it. If several rules try to catch the same ball, then the highest-priority one wins.

So when you say there is no direct correspondence between apply-templates and the template rule, you are spot on. That's the whole point, it's what makes the idea so powerful. It creates a very loose coupling between the code for processing one element and the code for processing its children, which means that the code is very resilient to changes in the document structure.

In your example there's no explicit rule that catches tennis balls labelled "publisher". But there's an implicit rule, which is to apply-templates to each of its children. If one of the children is a book element, that element will in turn be thrown over the wall, to be caught this time by the publisher/book template rule.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Formats carumuga SQL Server 2000 4 January 2nd, 2007 07:20 PM
Debit and credit formats Tachyophan Access VBA 0 October 24th, 2006 07:17 AM
Trouble with date formats DrewMills Access VBA 3 March 7th, 2005 06:22 PM
About Message formats abdul_owiusa C# 3 August 10th, 2004 12:46 PM
Exporting mysql to other formats fold1234 PHP Databases 5 December 5th, 2003 01:48 PM





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