Subject: Re: the joy of breaking out from procedural/imperative programming style (was: Re: [xsl] Peculiar Problem in .xsl file From: "Kurt Cagle" <cagle@xxxxxxxxx> Date: Fri, 6 Dec 2002 09:38:46 -0800 |
Gunther, Things get even better in XSLT2/Xpath2 <xsl:stylesheet ... xmlns:dt="http://www.mySchemas.com/date"> <xsl:function name=" dt:getMonthName" xmlns:dt="http://www.mySchemas.com/date"> <xsl:param name="date"/> <xsl:variable name="months" select="tokenize("January,February,March,April,May,June,July,August,Septembe r,October,November,December",',')"/> <xsl:return select="$months[number(substring($monthNum,5,2))]"/> </xsl:function> ... <xsl:value-of select="dt:getMonthName(current-dateTime())"/> ... </xsl:stylesheet> Functional Programming at its most basic. ----- Original Message ----- From: "Gunther Schadow" <gunther@xxxxxxxxxxxxxxxxxxxxxx> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>; "Chandra -" <pchandramohan@xxxxxxxxxxx> Sent: Friday, December 06, 2002 8:31 AM Subject: the joy of breaking out from procedural/imperative programming style (was: Re: [xsl] Peculiar Problem in .xsl file > Hi, on Chandra's issue of month names, etc. I'd like to take > the oportunity to do some XSLT style-evangelism. > > I think this case is a particular good example for how ugly > choices are and that one should deliberately try to find other > ways than using xsl:choose, (and xsl:call-template, xsl:for-each > for that matter.) > > XSLT is much more fun if you use its own powers rather than > trying to cling on to the weak metaphors of imperative programming > (i.e., case, procedure call and loops.) > > So, instead of this ugly choice, you could make tables: > > <xsl:variable name="monthNames"> > <month digits="01" name="January"/> > <month digits="02" name="February"/> > <month digits="03" name="March"/> > <month digits="04" name="April"/> > <month digits="05" name="May"/> > <month digits="06" name="June"/> > <month digits="07" name="July"/> > <month digits="08" name="August"/> > <month digits="09" name="September"/> > <month digits="10" name="October"/> > <month digits="11" name="November"/> > <month digits="12" name="December"/> > <xsl:variable> > > and then you simply go: > > <xsl:value-of > select="$monthNames/month[@digits=substring($date, 5, 2)]/@name"/> > > just think of relational model for most of your data, the above > is same as the SQL form > > SELECT name FROM monthNames WHERE digits='..' > > The advantage: much cleaner code, much less ugly choose/when, > easily reuseable, easily EXTENSIBLE. For instance you could > easily support multi-lingual date representations either by > loading the month name table from an external resource: > > <xsl:variable name="monthNames" select="document($monthnamesuri)"/> > > or by adding another "column" for language code such as: > > <month digits="01" name="January" xml:lang="en"/> > <month digits="01" name="Januar" xml:lang="dn"/> > <month digits="01" name="Janvier" xml:lang="fr"/> > > etc. > > Just indulge in the many possible ways you could read this data > from XML locale files. > > XSLT is so much fun if you can break out of the imperative/ > procedural progamming box. (And much pain and suffering if you > don't break out of that box.) > > enjoy, > -Gunther > > > Gunther Schadow wrote: > > > What you should try is this: > > > > <xsl:when test="$month='01'"> > > > > instead of <xsl:when test="$month=01"> > > > > that way you avoid the potentially buggy type casting behavior > > of your XSLT processor to become manifest. Just leave the values > > you compare both strings at all times (it's probably slightly > > faster that way anyway :-). That way you are sure to hold on to > > the leading zero. > > > > In fact I wonder if it's really buggy type casting? It seem to > > be reasonable for this machinery to cast the second operand to > > of the = relation to the data type of the first operand. That > > would cast the number 1 to a string, which would be '1', then > > you compare '01'='1' and the answer is correctly false. XSLT > > spec might make a ruling that clarifies that, but it's a subtle > > problem, so, I'd expect that to go wrong somewhere. Luckily > > your workaround is straight-forward. > > > > regards, > > -Gunther > > > > > > Chandra - wrote: > > > >> Hi Guys, > >> I am trying to print out a html file with a date in words, given date > >> in a xml file in number format > >> XSL file: > >> <xsl:variable name='month' select='substring ($date, 5, 2)'/> > >> <xsl:choose> > >> <xsl:when test='$month=01'> > >> <xsl:text>January </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=02'> > >> <xsl:text>February </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=03'> > >> <xsl:text>March </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=04'> > >> <xsl:text>April </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=05'> > >> <xsl:text>May </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=06'> > >> <xsl:text>June </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=07'> > >> <xsl:text>July </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=08'> > >> <xsl:text>August </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=09'> > >> <xsl:text>September </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=10'> > >> <xsl:text>October </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=11'> > >> <xsl:text>November </xsl:text> > >> </xsl:when> > >> <xsl:when test='$month=12'> > >> <xsl:text>December </xsl:text> > >> </xsl:when> > >> </xsl:choose> > >> > >> When valuepassed into the date variable is 20020516, I don't get a > >> month. Infact if the month is from 01 to 09, I dont get a month in > >> words. If the month is 10 to 12, I get a month displayed. Very very > >> peculiar. I am unable to find the solution. I tried and printed out > >> the value of the month variable. It seems to take the 2 digits > >> correctly. However it doesn't go into the appropraite month code when > >> the month starts with 0!! For example, if the month is 05, it doesnt > >> go into this part of the code: > >> <xsl:when test='$month=05'> > >> <xsl:text>May </xsl:text> > >> </xsl:when> > >> If the month were 10, 11 or 12, the code works fine. > >> Can anyone guess what the problem is... > >> Thanks in advance, > >> Chandra > >> > >> > >> > >> _________________________________________________________________ > >> The new MSN 8: advanced junk mail protection and 2 months FREE* > >> http://join.msn.com/?page=features/junkmail > >> > >> > >> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > > > > > > > > -- > Gunther Schadow, M.D., Ph.D. gschadow@xxxxxxxxxxxxxxx > Medical Information Scientist Regenstrief Institute for Health Care > Adjunct Assistant Professor Indiana University School of Medicine > tel:1(317)630-7960 http://aurora.regenstrief.org > > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: the joy of breaking out from pr, David Carlisle | Thread | Re: the joy of breaking out from pr, David Carlisle |
Re: the future (was RE: [xsl] Locat, Kurt Cagle | Date | Re: the joy of breaking out from pr, David Carlisle |
Month |