Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2007, 01:35 PM
Registered User
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stripping last X characters

Newbie here, I have an easy question for you all.

How do I strip the last, say 3, characters of a string in XSLT?

Thanks much.

 
Old September 21st, 2007, 03:17 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to combine the substring and string-length functions:
Code:
<xsl:template name="strip-end-characters">
    <xsl:param name="text"/>
    <xsl:param name="strip-count"/>
    <xsl:value-of select="substring($text, 1, string-length($text) - $strip-count)"/>
  </xsl:template>
Full example:
Code:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title">
    <xsl:call-template name="strip-end-characters">
      <xsl:with-param name="text" select="."/>
      <xsl:with-param name="strip-count" select="3"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="strip-end-characters">
    <xsl:param name="text"/>
    <xsl:param name="strip-count"/>
    <xsl:value-of select="substring($text, 1, string-length($text) - $strip-count)"/>
  </xsl:template>
</xsl:stylesheet>
If using XSLT 2.0 you'd probably rewrite this as an xsl:function.
(Change the match="title" to match any text containing element and run against your XML.)


--

Joe (Microsoft MVP - XML)
 
Old September 21st, 2007, 05:11 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

substring($in, 1, string-length($in)-3)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping Data from a Field jjc9809 Access VBA 1 July 12th, 2008 08:16 PM
Stripping CRLF?? crabjoe Classic ASP Basics 7 February 26th, 2008 05:58 PM
Stripping hyphens arholly Access 2 January 4th, 2007 09:16 AM
regex for stripping space muki XSLT 2 February 8th, 2006 06:29 AM
Stripping down variable values mellan XSLT 1 September 19th, 2005 09:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.