Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] starts-with on more than one item

From: Wendell Piez <wapiez@---------------->
To:
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
======================================================================


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent