Re: [xsl] Testing the Last Character of a String

Subject: Re: [xsl] Testing the Last Character of a String
From: "Tony Graham" <tgraham@xxxxxxxxxx>
Date: Thu, 1 Aug 2013 16:44:49 +0100 (IST)
On Thu, August 1, 2013 3:55 pm, Nathan Tallman wrote:
> I'd like to test the last character in a node text-string. If it's a
> period, then insert the value and a space. If it's not a period, then
> insert the value, a period., and a space. I think I've gotten most of
> the code right, but don't have it quite right, as it's not working. Any
> tips?
>
> Thanks,
> Nathan
>
> <xsl:choose>
>     <xsl:when test="substring(archdesc/did/unittitle/text(),
> string-length(archdesc/did/unittitle), 1)=.">

This won't do the right thing if unittitle can end with '?', '!', or any
other punctuation character.

If unittitle can contain elements as well as text (i.e., contain mixed
content) then 'substring()' will blow up when you select multiple text
nodes.

>         <xsl:value-of select="normalize-space(archdesc/did/unittitle)"/>

And you'll lose the child elements, if any exist, when you do the
'normalize-space()'.

>         <xsl:text>&#160;</xsl:text>

As Liam said, it's redundant to repeat the '<xsl:text>&#160;</xsl:text>'.

>     </xsl:when>
>     <xsl:otherwise>
>         <xsl:value-of select="normalize-space(archdesc/did/unittitle)"/>
>         <xsl:text>.&#160;</xsl:text>
>     </xsl:otherwise>
> </xsl:choose>

Assuming there are no child elements but the title can end with other
punctuation characters:

<xsl:value-of select="normalize-space(archdesc/did/unittitle)"/>
<xsl:if test="not(matches(normalize-space(archdesc/did/unittitle),
'[.?!]$'))">
  <xsl:text>.</xsl:text>
</xsl:if>
<xsl:text>&#160;</xsl:text>

Regards,


Tony Graham                                   tgraham@xxxxxxxxxx
Consultant                                 http://www.mentea.net
Mentea       13 Kelly's Bay Beach, Skerries, Co. Dublin, Ireland
 --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
    XML, XSL-FO and XSLT consulting, training and programming

Current Thread