Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

confused about <apply-templates/> 1

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
XML
===

<?xml version="1.0"?>

<message priority="low">Hey, XSLT isn't so hard after all!</message>


XSLT
====

<stylesheet version="1.0" xmlns="<output method="text"/>

<template match="message">
<apply-templates/>
</template>

</stylesheet>


output
=======
Hey, XSLT isn't so hard after all!


how "<apply-templates/>" works ? i am very very much confused about its functioning.

It seems to me the role of <apply-templates/> changes time to time depending upon its position inside XSLT. is it correct ?


does it follow any rule ?

please explain how "<apply-templates/>" works ?
 
Stylesheets work by outputting the contents of a template when an XML node matches for that template. The parser will work through the nodes from top to bottom. If a template is not found for a particular node, the parser will use the default template:
Code:
<xsl:template>
  <xsl:value-of select="."/>
</xsl:template>
Consider the stylesheet:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/"/>
  <xsl:template match="message">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
The first template matches the document root and its children, so all the nodes are catered for and the parser will output nothing. Now, if you use apply templates, you are telling the parser to examine child nodes and try to match further templates against them. So:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="message">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
will match the document root, then look for further templates for its children. "message" will match, therefore the template will be output.

You can be more specific when applying templates:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates select="message"/>
  </xsl:template>
  <xsl:template match="message">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
hi, i have not read your comment yet. but i will read now.

i have got one more trouble so let me put that here.

i am not able to debug .

i have a things like,

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="
<xsl:template match="blah">
<xsl:apply-templates select="/oneSpecifiedNode"/>


</xsl:template>


<xsl:template match="/oneSpecifiedNode" >

// do some work here===>line-1


</xsl:template>

</xsl:stylesheet>



It compiled and run . but i did not see any output from //line-1.

i am very much upset why //line-1 part is not producing output. plz tell me how do you debug whats wrong happening here ? why its not outputting ? at lest tell me the way to debug.


thanks for you help. i am glad that you are helping me.
thank you very much
 
Start off with:
Code:
<xsl:template match="/">
  <xsl:apply-templates select="mypath/mynode"/>
</xsl:template>
<xsl:template match="mynode">
test
</xsl:template>
match "/" means match the document root. If you don't get an output, its because mypath/mynode bit is wrong.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
...Or if your lazy like me ;-)

select="//mynode"

Which ignores the Path and selects all <mynode> tags in the document...

Warning: Depending on the structure of your document, this could have adverse effects... so proceed with caution ;-)

Such as in...
Code:
<A>
  <mynode />
  <B>
    <mynode />
  </B>
  <B>
    <mynode />
  </B>
  <B>
    <C>
      <mynode />
    </C>
  </B>
</A>

select="//mynode" will select all 4 instances...
Where select="A/B/mynode" only will select 2

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
@jonty

i have some doubt on your comment.
should i ask or should not i ? Ok, if i dont ask, i wont be able to learn,it'll create more confusion, so i am asking my sily question


i could not understand

You have mentiond
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/"/>
  <xsl:template match="message">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>
"The first template matches the document root and its children, so all the nodes are catered for and the parser will output nothing"

And here is the trouble. i simply could notunderstand why there will be no output . you have given an explanation which i could not follow.

You know, while i saw this snippet, i just thought that it would be a compile error because its nesting of templates.
later on i thought well, compiler may pass it and produce a
output "Hey, XSLT isn't so hard after all!" because we are using
Code:
<xsl:template match="message">
    <xsl:value-of select="."/>

But you are saying there will be no output !!! [sad]
Its difficult to understand this fact. will you please simplify the matter ? or tell me whats wrong in my approach ?

thank you for your kind cooperation.
thanks @Cube for the tutorial. thats a very good one.
 
Firstly Cube. It is very bad practice to use the "//" and should only really be used when the XML structure is unknown. This searches all the nodes in the tree, and if your XML file is large or you use // a lot in the stylesheet, it will be very slow. There is almost always a better option.

Secondly satellite. There is no nesting of templates in that example. I'll try to explain again.

XSL works by parsing the XML tree, looking at each node and finding a template from the stylesheet so it knows how to output it. The point of the example was that the parser will match all the nodes using the <xsl:template match="/"/> template. So the parser is satisfied it has processed all the nodes, so it effectively ignores the next template (message has already been matched).

<xsl:apply-templates/> is a command that says to the parser "hang on, I want you to keep looking for another template to match these nodes".

No such thing as a silly question.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
@jonty

it was a very beautiful explanation. things are ok now.its quiet interesting.

thank you very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top