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 6009925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:02:31+00:00 2026-05-23T02:02:31+00:00

I have recently started using XSLT for some of my XML documents and I

  • 0

I have recently started using XSLT for some of my XML documents and I have some questions. I add the code below. In the code I have a template that matches ebook elements. I then want to list all the authors that wrote the book. I do it using a for each loop, but I could also apply a template to it. I can’t see a clear line when to use loops and when to use templates.

And another question is it normal to just say apply-templates when you now that there wont be other children of the element where you are writing it. In my case in the template that matches the document root I say apply-templates. Then it finds ebooks which is the only child of it, but I could have a “books” element which distinguish between “regular” books, and electronic books then it would just list the character data of the books. I then would have needed to write apply-templates select=”ebooks” if I just wanted the ebooks in my final document. So is this a case of that it depends of how well you know your document?

Thank you, here is my code (This is just for practicing):

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ebooks.xsl"?>
<ebooks>
    <ebook>
        <title>Advanced Rails Recipes: 84 New Ways to Build Stunning Rails Apps</title>
        <authors>
            <author><name>Mike Clark</name></author>
        </authors>
        <pages>464</pages>
        <isbn>978-0-9787-3922-5</isbn>
        <programming_language>Ruby</programming_language>
        <date>
            <year>2008</year>
            <month>5</month>
            <day>1</day>
        </date>
        <publisher>The Pragmatic Programmers</publisher>
    </ebook>
    ...

XSLT:

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

    <xsl:template match="/">
        <html>
            <head>
                <title>Library</title>
            </head>
            <body>
                <xsl:apply-templates />            
            </body>
        </html>    
    </xsl:template>

    <xsl:template match="ebooks">
        <h1>Ebooks</h1>
        <xsl:apply-templates>
            <xsl:sort select="title"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="ebook">
        <h3><xsl:value-of select="title"/></h3>
        <xsl:apply-templates select="date" />

        <xsl:for-each select="authors/author/name">
            <b><xsl:value-of select="."/>,</b>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="date">
        <table border="1">
            <tr>
                <th>Day</th>
                <th>Month</th>
                <th>Year</th>
            </tr>
            <tr>
                <td><xsl:value-of select="day"/></td>
                <td><xsl:value-of select="month"/></td>
                <td><xsl:value-of select="year"/></td>
            </tr>
        </table>
    </xsl:template>

</xsl:stylesheet>
  • 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-23T02:02:32+00:00Added an answer on May 23, 2026 at 2:02 am

    This question is a bit argumentative, but here is my take on it.

    I can’t see a clear line when to use
    loops and when to use templates.

    I’d say that you shoud strive to avoid for-each as often as possible in favor of apply-templates.

    Using for-each makes your program more complex by adding nesting levels and it’s also impossibe to re-use the code inside the for-each block. Using apply-templates will (when done right) generate more more flexible and modular XSLT.

    On the other hand: If you write a stylesheet with limited complexity and re-usability or modualarization are not a concern, using for-each may be quicker and easier to follow (for a human reader/maintainer). So it’s partly a question of personal preference.

    In your case, I would find this elegant:

    <xsl:template match="ebook">
      <!-- ... -->
      <xsl:apply-templates select="authors/author" />
      <!-- ... -->
    </xsl:template>
    
    <xsl:template match="authors/author">
      <b>
        <xsl:value-of select="name"/>
        <xsl:if test="position() &lt; last()">,</xsl:if>
      </b>
    </xsl:template>
    

    To your other question

    And another question is it normal to just say apply-templates when you know that there won’t be other children of the element where you are writing it.

    When you know there will never be any children in this element, writing apply-templates is pointless.

    When done right, XSLT has the ability to flexibly deal with varying input. If you expect there could be children at some point, an apply-templates won’t hurt.

    A simple apply-templates without select is rather unspecific, both in terms of which (i.e.: all of them) and in what order (i.e.: input document order) nodes will be processed. So you could end up either processing nodes you never wanted to process or node you already have processed earlier.

    Since one cannot write a sensible template for unknown nodes, I tend to avoid the unspecific apply-templates and just adapt my stylesheet when the input changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started using DocCheck for checking the validity of JavaDoc's in code
We have recently started using FxCop on our code base and I am in
I have recently started to learn Objective-C and write my tests using OCUnit that
I have recently started using coffeescript with Rails and I am finding that sometimes
I have recently started parsing JSON documents using SBJSON Parser, and I am able
I have recently started a tutorial to learn how to code GUI using Windows
I have recently started looking into using Azure but I'm having some issues getting
I have recently started using generics in java, and in that attempt tried to
I recently started using WPF and the MVVM framework, one thing that I have
I have recently started using Vim as my text editor and am currently working

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.