xsl-list
[Top] [All Lists]

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

2013-08-01 10:04:52
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   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>
</xsl:if>

<xsl:text> </xsl:text>

Liam

-- 
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


--~------------------------------------------------------------------
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>