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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A newbie question regarding the <xsl:apply-templates> element. 1

Status
Not open for further replies.

kubu

Programmer
Jan 10, 2001
23
US
I was reading Bob DuCharme's article titled "Sorting in XSLT" ( I saw the following code that causes me some confusion:

<xsl:template match=&quot;employees&quot;>
<xsl:apply-templates>
<xsl:sort select=&quot;salary&quot;/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match=&quot;employee&quot;>
Last: <xsl:apply-templates select=&quot;last&quot;/>
First: <xsl:apply-templates select=&quot;first&quot;/>
Salary: <xsl:apply-templates select=&quot;salary&quot;/>
Hire Date: <xsl:apply-templates select=&quot;@hireDate&quot;/>
<xsl:text>
</xsl:text>

</xsl:template>


My question is, what is the purpose of using <xsl:apply-templates select=&quot;{node}&quot;/> for a selected node when you haven't defined a specific template to match it? For example, the XSL element above --

<xsl:apply-templates select=&quot;last&quot;/>

-- doesn't seem to be referencing a specific template. So, what template(s) is it applying? Wouldn't using the this --

<xsl:value-of select=&quot;last&quot;/>

-- give the save results?

Am I just missing some subtle functionality of this element that makes it a better choice in this situation? I thought I understood when to use <xsl:apply-templates>, but now I'm not so sure.

thanx,
Mark
 
If no template is specified in an XSL document for an element, a default rendering is performed. The default is just printing out the text contents of the element.

In your case, there is probably no difference between using apply-templates and value-of, but if the XSL designer just wants the text hidden within a complex element and doesn't know how deep in the XML structure the text is - apply-templates just streams it out. For example, if salary is structured like this:

<salary>
<yearly>120000</yearly>
<monthly>10000</monthly>
<maxbonus type=&quot;percentage&quot;>15</maxbonus>
</salary>

<xsl:value-of select=&quot;salary&quot;/> produces nothing, but apply-templates produces the not very comprehensible &quot;1200001000015&quot; - so at least you'd see something. A better example would have had just one text item a few levels down.

Another possible use for using apply-templates without a defined template is for XSL files that xsl:import or xsl:include other stylesheets or are included or imported into other stylesheets. The missing templates may be defined elsewhere.

In general, for your example, it is just a matter of preference (and maybe how often the data structure might change).

Uura
~~~~
&quot;Common sense tells you that the world is flat.&quot;
 
Thanks for your reply, uura. Your example really helped. It makes a little more sense to me now.

I also posted this same question in Wrox's XSLT discussion forum (p2p.wrox.com), and for what it's worth, here's the answer I received:

-------------------

Basically, it gives you a bit more flexibility. If you don't want to use the built-in rules (see below), you can override them by adding a new template and not having to change your existing code.

> For example, the XSL element above --
>
> <xsl:apply-templates select=&quot;last&quot;/>
>
> -- doesn't seem to be referencing a specific template. So,
> what template(s) is it applying?

It is using the built-in rules.
See:
> Wouldn't using the this --
>
> <xsl:value-of select=&quot;last&quot;/>
>
> -- give the save results?

If you follow the built-in rules, you will see that it will eventually produce the same results as your above statement.

-------------------

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top