[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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

Subject: Re: [xsl] Testing the Last Character of a String
From: Liam R E Quin <liam@xxxxxx>
Date: Thu, 01 Aug 2013 11:03:46 -0400

On Thu, 2013-08-01 at 10:55 -0400, 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?

> string-length(archdesc/did/unittitle), 1)=.">

In XPath, . is the current node, and '.' is a string consisting of a
full stop (US: Period).

Note also &#160; is not a space - it's a non-breaking space.
I'd put <xsl:text> </xsl:text> after the end of the xsl:choose element
if you want a normal space, since you want it in both cases. Yuo coud
then use an if instead of a choose. Note also that if the value ends in
a space, you use normalize-space to strip leading and trailing spaces
and collapse internal sequences of blanks to a single space, but you do
that _after_ looking at the last character.

<xsl:variable name="no-spaces"
   select="normalize-space(archdesc/did/unittitle)" />

<xsl:value-of select="$no-spaces" />

<xsl:if test="substring($no-spaces, string-length($no-spaces), 1) != '.'")>
  <!--* append a . if there was a non-dot at the end *-->

<xsl:text> </xsl:text>


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml

Current Thread