Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6017897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:11:44+00:00 2026-05-23T03:11:44+00:00

I am trying to understand apply-templates but I do not understand why I don’t

  • 0

I am trying to understand apply-templates but I do not understand why I don’t write any select=”nodename” in the apply-templates here: (I think of the first apply-templates beneath the My CD collection)

Snippet from the input document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>

XSLT:

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

Taken from the w3schools tutorial. How does it understand which of the templates it should choose?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:11:45+00:00Added an answer on May 23, 2026 at 3:11 am

    As by the specs:

    In the absence of a select attribute, the xsl:apply-templates instruction processes all of the children of the current node, including text nodes.

    apply-templates without XPath selection, applies the templates following the hierarchy of the XML tree view built by the processor during compilation, unless you explicitly drives the templates (as you did for title and artist).

    You might also want to think of how built-in template rules work. These rules run behind the scenes and allows recursive process go on in absence of a successful pattern match.

    So if you omit the template match for the root / your templates will get executed anyway, thanks to the built-in rules.

    I think the processing order should be like this:

    • The template matches the root, and xsl:apply-templates tells the processor to apply the templates to the catalog element (in the place where it is invoked).
    • No matching templates are found for catalog, so that the built-in rule allows continue processing toward the other descendant elements (catalog) until a new template with succesfull pattern match is found (cd)

    The built-in rule runs behind the scenes and you must think always your transform as composed by your templates, plus a few additional hidden (but working) templates:

    <xsl:template match="*|/">
      <xsl:apply-templates />
    </xsl:template>
    
    <xsl:template match="text()|@*">
      <xsl:value-of select="."/>
    </xsl:template>
    
    <xsl:template match="processing-instruction()|comment()"/>
    

    In your specific case the former template among the three above was the one resonsible for applying templates to the cd elements.

    These built-in templates gets overridden each time you write an explicit template.


    Examples

    You can obtain the same by replacing:

    <xsl:template match="cd">
        <p>
            <xsl:apply-templates select="title"/>
            <xsl:apply-templates select="artist"/>
        </p>
    </xsl:template>
    

    with:

    <xsl:template match="country|company|price|year"/>
    
    <xsl:template match="cd">
        <p>
            <xsl:apply-templates />
        </p>
    </xsl:template>
    

    About the root, in your case, you could also obtain the same by replacing:

    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    

    with

    <xsl:template match="/catalog">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    

    or still:

    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <xsl:apply-templates select="catalog"/>
            </body>
        </html>
    </xsl:template>
    

    or still:

    <xsl:template match="catalog">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <xsl:apply-templates />
            </body>
        </html>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
I'm studying apply and I am trying to understand why the code I am
I'm new to GWT and trying to understand it, but have encountered with a
I'm testing some code(trying to make it faster but also trying to understand the
I'm testing the following and trying to understand what it does to then apply
I'm trying to understand the tspan tag in svg and here is something weird.
The problem i am trying to understand is easy but i cant seem to
I'm trying to understand some of the function in WordPress, but I can't get
Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/ ): object io {
This is a followup to this question. Here's the code I'm trying to understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.