XSLT default value

Posted on 2011-05-10
Medium Priority
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
  • 2
LVL 60

Accepted Solution

Geert Bormans earned 2000 total points
ID: 35729873
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
ID: 35729891
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

840 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