xsl-list
[Top] [All Lists]

RE: [xsl] select first word

2007-12-18 10:33:41
If we don't care about preserving the commas, we could just translate them to 
spaces to start with, and simply check for the space after that. Then it's not 
dependent on a space being followed by a comma. Something like this:

<xsl:variable name="trans" select="translate(., 
'áâãäåèéêëìíîïòóôõöùúûüÈÉÊËÁÀ,', 'aaaaaeeeeiiiioooooEEEEAA ')" />
<xsl:choose>
        <xsl:when test="contains($trans, ' ')">
                <xsl:value-of select="substring-before($trans, ' ')" />
        </xsl:when>
        <xsl:otherwise>
                <xsl:value-of select="$trans" />
        </xsl:otherwise>
</xsl:choose>


~ Scott


-----Original Message-----
From: Abel Braaksma [mailto:abel(_dot_)online(_at_)xs4all(_dot_)nl] 
Sent: Tuesday, December 18, 2007 11:12 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] select first word

Florent Georges wrote:
Abel Braaksma wrote:

  
<xsl:value-of select="
        translate(
           substring-before(
               substring-before(.,' '), ', '),
        $from, $to)" />
    

  If I am right (if I remember correctly) the input doesn't always have
space or comma.  So I would use the following instead (or the result
could be empty):

    translate(replace(., '([^, ]+).*', '$1'), $from, $to)

Thanks for pointing that out.
Yes, you are right about the input. But the OP has, I believe (from 
observation), only XSLT 1.0 and replace() is introduced as per XSLT 2.0.

The substring-before with the comma can be removed: if there's a comma 
it is followed by a space. Just add the comma to the translate function 
at the end, then it will be removed.

For the rest I'd probably choose something like this:

<!-- include comma, which is not matched, thus it will be deleted -->
<xsl:variable name="from" select="'áâãäåèéêëìíîïòóôõöùúûüÈÉÊËÁÀ,'" />
<xsl:variable name="to" select="'aaaaaeeeeiiiioooooEEEEAA'" />

<!-- with space -->
<xsl:template match="le[contains(., ' ')]">
    <xsl:value-of select="translate(substring-before(., ' '), $from, 
$to)" />
</xsl:template>

<!-- without space -->
<xsl:template match="le">
    <xsl:value-of select="translate(., $from, $to)" />
</xsl:template>

The second one is the more generic one and will be called by the 
processor if there's no space in the content of the <le> element.

HTH,

Cheers,
-- Abel Braaksma




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>