xsl-list
[Top] [All Lists]

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

2013-08-01 10:45:01
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(_at_)mentea(_dot_)net
Consultant                                 http://www.mentea.net
Mentea       13 Kelly's Bay Beach, Skerries, Co. Dublin, Ireland
 --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
    XML, XSL-FO and XSLT consulting, training and programming



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