[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Testing the Last Character of a String
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> </xsl:text> As Liam said, it's redundant to repeat the '<xsl:text> </xsl:text>'. > </xsl:when> > <xsl:otherwise> > <xsl:value-of select="normalize-space(archdesc/did/unittitle)"/> > <xsl:text>. </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> </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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|