Altova Mailing List Archives>Archive Index >xsl-list Archive Home >Recent entries >Thread Prev - [xsl] starts-with on more than one item [Thread Next] Re: [xsl] starts-with on more than one itemTo: Date: 6/2/2008 4:30:00 PM At 07:10 PM 5/30/2008, David wrote:
glad you got it working, just one comment,
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:value-of select="concat(', and ', ., '.')"/>
</xsl:when>
<xsl:when test="position()=1">
<xsl:value-of select="concat(' ', .)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(', ', .)"/>
</xsl:otherwise>
</xsl:choose>
swap the first two when clauses, otherwise you'll get an ", and" on a list
of length 1.
If it wasn't for the and you could replace the whole for-each by
<xsl:value-of select="$names" separator=", "/>
It's probably possible to use a trick xpath to get the same effect with
a single value-of, but it's probably more maintainable to use the
for-each form that you have in this case.
Even without being terribly tricky, one might refactor and extend as
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:text> </xsl:text>
<xsl:otherwise>
<xsl:when test="position()=last()">
<xsl:if test="position() > 2">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:text> and </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
As David says, moving the position()=1 test accounts for the case of
a set with one member. Adding a test inside the position()=last()
clause accounts for the case of two members, which presumably
shouldn't get a comma between them.
The problem of transposing from singular to plural forms reliably in
English requires either NLP or at least a lookup table with all your
possible terms; it's usually best dealt with by writing around it.
E.g., not "router, tranformers, and chassis" but "routers (1),
transformers (4), and chassis (3)". It's not elegant, but it's clear
enough and not obviously wrong either.
Cheers,
Wendell
======================================================================
Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc. http://www.mulberrytech.com
17 West Jefferson Street Direct Phone: 301/315-9635
Suite 207 Phone: 301/315-9631
Rockville, MD 20850 Fax: 301/315-8285
----------------------------------------------------------------------
Mulberry Technologies: A Consultancy Specializing in SGML and XML
====================================================================== | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
