[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: starts-with on more than one item

Subject: Re: starts-with on more than one item
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 02 Jun 2008 11:49:41 -0400
Re:  starts-with on more than one item
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
======================================================================

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.