Re: [xsl] The Oxford Comma - A Gift Worth Atleast 5 Cents

Subject: Re: [xsl] The Oxford Comma - A Gift Worth Atleast 5 Cents
From: ac <ac@xxxxxxxxxxxxx>
Date: Thu, 19 Jun 2008 23:46:28 -0400
Hi Ronnie & List,

I think that I might have written it somewhat like this in xslt2:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0"
   xmlns:javascript="javascript:code"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:xs="http://www.w3.org/2001/XMLSchema";
   xmlns:xfn="http://www.w3.org/2005/xpath-functions";
   xmlns:xdt="http://www.w3.org/2005/xpath-datatypes";
   xmlns:saxon="http://saxon.sf.net/"; extension-element-prefixes="saxon"
   exclude-result-prefixes="saxon xfn xdt xs xsl javascript">

<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="training-names" select="
('Cisco IP phone end user training',
'Cisco attendant console operator training',
'Cisco call center agent training')"/>
<xsl:variable name="cnt" select="count($training-names)"/>
<xsl:variable name="services">
<xsl:for-each select="$training-names">
<xsl:value-of select="string-join((
if ($cnt lt 2 or ($cnt eq 2 and position() ne 1))
then (' ', ., '. ')
else if ($cnt eq 2)
then (' ', ., ' and')
else if (position() eq last())
then (' and ', ., '. ')
else (' ', ., ',')), '')"/>
</xsl:for-each>
</xsl:variable>
<xsl:message>The services also include<xsl:value-of select="$services"/></xsl:message>
</xsl:template>


</xsl:stylesheet>

and that, although English is not the worse, human languages have relatively little to do with logic and structure since they are built from arbitrary usage and tradition, not structure and logic (although some have tried). Esperanto is the best. Yet, as some have proposed, its accented letters could easily be replaced with unaccented letters, making digital life easier for users, if it was not for the Esperanto tradition ...

Programming languages always carry quite a bit of tradition also but at least, they usually have some structure and logic. Standardized and XML-based, XSLT (2) is quite nice to process and transform, at least relatively to most other languages.

Cheers,
ac


Ronnie Royston a icrit :
To my esteemed XSLT developers, I submit this simple gift you you with
humility.  If you ever end up with a variable containing a sequence of
3 or more English words/phrases and want to output good English, paste
this in (just change the variable name to whatever).

I know this is childs play for all you intelligent folks but hey,
maybe it will save someone 10 minutes of typing one day?  Just trying
to give back to the group that is able to deliver the seemingly
impossible..(David)

OUTPUTS: "The services also include Cisco IP phone end user training,
Cisco attendant console operator training, and Cisco call center agent
training."

The services also include
<xsl:for-each select="$trainingnames">
<xsl:choose>
    <xsl:when test="count($trainingnames) > 2">
        <xsl:choose>
        <xsl:when test="position()=1">
            <xsl:value-of select="concat(' ', .,',')"/>
        </xsl:when>
        <xsl:when test="position()=last()">
            <xsl:value-of select="concat(' and ', .,'. ')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat(' ', .,',')"/>
        </xsl:otherwise>
        </xsl:choose>
    </xsl:when>
    <xsl:when test="count($trainingnames) = 2">
        <xsl:choose>
        <xsl:when test="position()=1">
            <xsl:value-of select="concat(' ', .,' and')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat(' ', .,'. ')"/>
        </xsl:otherwise>
        </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="concat(' ', .,'. ')"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:for-each>

Current Thread