Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language XQuery

(Version using iterative FLOWR)

Date:12/28/05
Author:David Sewell
URL:n/a
Comments:0
Info:http://www.w3.org/TR/xquery/
Score: (2.90 in 10 votes)
(: XQuery version of 99 Bottles of Beer on the Wall
   Based on 3 November 2005 Candidate Recommendation syntax; 
   see http://www.w3.org/TR/2005/CR-xquery-20051103/
   Coded by David Sewell, dsewell@virginia.edu - 2005-12-27
   
   This version iterates over fn:reverse(0 to 99) rather than
   using a recursive function (an equally XQuery-ish approach).
:)

declare namespace my = "http://www.99-bottles-of-beer.net/xquery";

declare function my:plural($bottles as xs:integer?) as xs:string
{  if ($bottles eq 1) then "" else "s" };

<song>{
   for $b in reverse(0 to 99)
   let $howmanyBegin :=  if ($b > 0) then        string($b)
                         else                    "No more"
   let $howmanyEnd   :=  if ($b > 1) then        string($b - 1)
                         else if ($b eq 1) then  "No more"
                         else                    "99"
   let $pronoun      :=  if ($b eq 1) then       "it"
                         else                    "one"
   let $action :=        if ($b > 0) then concat("Take ", $pronoun, " down and pass it around,")
                         else                    "Let's go to the store and buy some more,"
   return
       <verse>
          <line>{$howmanyBegin} bottle{my:plural($b)} of beer on the wall,</line>
          <line>{$howmanyBegin} bottle{my:plural($b)} of beer!</line>
          <line>{$action}</line>
          <line>{$howmanyEnd} bottle{my:plural($b - 1)} of beer on the wall!</line>
       </verse>
}</song>

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
The XML Query LanguageMartin Probst10/23/050

Comments

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: