XSLT default value

Posted on 2011-05-10
Last Modified: 2013-11-18
I've had it with this stupid language. How hard can it be to specify a default value for an attribute? It's frustrating beyond compare. Bah.

Anyway, I have a node. This node may or may not have an attribute called "render". When there is no such attribute, I want my to output "true", when there I have this attribute, I want to output its value.

I came up with something like this:

"render" : "<xsl:apply-templates select="list/entity/@render" mode="renderAttribute"/>"

Open in new window

and the template is:

    <xsl:template  name="renderAttribute">
        <xsl:when test="">
          <xsl:value-of select="true" />
          <xsl:value-of select="." />

Open in new window

Except it doesn't work. How do I make it work?
Question by:Emanuele_Ciriachi
    LVL 60

    Accepted Solution

    as a defence to the language (which I don't consider "stupid") I see some sources of misunderstanding in your code,
    so reading through an instructive book would not hurt your evolution in liking the language

    well, the template will not fire for four reasons
    - apply-template pushes out nodes to the templates with a @match, not to named templates
    - the @mode you have does not correspond with the one (empty or default in the template
    - for calling a named template (one with an attribute "name" instead of "match") you need call-template
    - if you apply template with a potential empty node, nothing will happen

    this is what I would do

    "render" : "<xsl:apply-templates select="list/entity" mode="renderAttribute"/>"
    and then

    <xsl:template  match="entity[@render]" mode="renderAttribute">
      <xsl:value-of select="@render"/>

    <xsl:template  match="entity[not(@render)]" mode="renderAttribute">
    LVL 60

    Expert Comment

    by:Geert Bormans
    and if you are willing to invest in your relation with the language

    this one is a very good learning book
    the latest edition also covers XSLT2

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now