Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Locate the Design Checker Task Pane in InfoPath 2010
Locate Conditional Formatting and Custom Validation in InfoPath 2010
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
5 MOST POPULAR
Access a Method in a Master Page with Code-Behind
Pass Query String Parameters to an ASP.NET Xml Control
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Configure IIS 7 for Custom Error Pages
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Group Dates by Year, Month and Day
Configure Lab Color Mode Defaults for Braintrove Articles
Create a Monochromatic Dream Text Effect Using Styles
Combine Multiple Sums Into a Single Function
Books
Web Standards Programmer's Reference : HTML, CSS, JavaScript, Perl, Python, and PHP
Learning Web Design : A Beginner's Guide to HTML, Graphics, and Beyond
HTML 4 For Dummies
XSLT Quickly
Learning XSLT
Magazines
.net
Visual Basic & Net Journal
MSDN Magazine
Inside Web Development
Asp.net Professional
Microsoft MVP
This article has been viewed 340 times.

Conditionally Display the Singular or Plural Form of Words

Page 1 of 4
Written by Gregory Scot Collins
Tuesday, 30 January 2007, 5:33 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
When displaying counts of items, we must decide how to deal with the need to display singular and plural items. There are a number of ways to deal with the issue. One common approach is to simply avoid the issue by not using words that can be represented in both the singular and plural. For example if we are displaying the number of widgets an online store has in stock, we can avoid plurality by just stating the number in stock: "1 in stock", "4 in stock", etc. Another way to avoid the issue is with the common and acceptable approach of using parentheses: "1 widget(s) in stock", "4 widget(s) in stock", etc.
While both of the above approaches have their place, it is not always desirable or acceptable to avoid the issue. Since XSL transforms have the capability to conditionally output content, we can take advantage of this to make our presentation more appealing. We will demonstrate two approaches, both accomplishing the same task with the same results. The first approach will follow the traditional use of XSLT to accomplish the task, while the second will use a more innovative, compact approach.
When there is one widget in stock, we want to output, "There is 1 widget in stock," and when there is zero or more than one widget in stock, we want to output, for example, "There are 4 widgets in stock". So we need to conditionally display "is" or "are" and "widget" or "widgets" based on the in stock count.
Because outputting to an attribute, such as an HTML tooltip, is different than outputting an element, we will write our transform to output to both. As you will see, outputting to an attribute using the compact approach is a much easier exercise; your code is less cluttered, and in my opinion, easier to read.

Create the Widgets.xml file

We need a simple XML file to transform that contains a list of widgets for sale and how many are in stock so that we can demonstrate displaying plurality conditionally. Copy the contents of Listing 1 into a text editor, and then save the file as Widgets.xml.
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Listing 1 - The Widgets.xml file.
Listing 2 - The TraditionalPlurals.xsl file.
Listing 3 - The CompactPlurals.xsl file.
Article Tags
Great Deals
TigerDirect Exclusive Deals, Limited Time Offers, Act Now And Save!
Find all current special offers on Adobe products.
Try SugarSync Free!
Join WebHost4Life.com
TigerDirect
Computers4SURE (4SURE.com - An Office Depot Co.)
Copyright © 2006-2010 Braintrove. All rights reserved. Braintrove, braintrove.com, and the Braintrove logo are trademarks of Gregory Scot Collins in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Any rights not expressly granted herein are reserved.