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 June 13th, 2007, 04:29 PM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default String manipulation

Is there a better/more efficient way to convert:

Projects\Documents (...regex...)
TO
Projects\\Documents (...regex...)


<xsl:template name="escape_backslash">
  <xsl:param name="in"/>
  <xsl:param name="out"/>
  <xsl:choose>
    <xsl:when test="contains($in, '\')">
      <xsl:call-template name="escape_backslash">
        <xsl:with-param name="in" select="substring-after($in, '\')"/>
        <xsl:with-param name="out" select="concat($out, substring-before($in, '\'), '\\')"/>
      </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="concat($out, $in)"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

 
Old June 13th, 2007, 05:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Unless you move to XSLT 2.0, you're stuck with this kind of approach.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 13th, 2007, 05:33 PM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, should rephrased question. Is there a more efficient way to do this in XSLT 2.0? I am using Saxon8.

 
Old June 13th, 2007, 05:41 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 it's

replace($in, '\\', '\\\\')

(in both the regex and the replacement string, a backslash is written as '\\')

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 14th, 2007, 09:25 AM
Authorized User
 
Join Date: Jun 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does it cost additional processing time if I defined it as a function and call it?

pc:escapeBackslash($path)

<xsl:function name="pc:escapeBackslash">
<xsl:param name="$in"/>
<xsl:sequence select="replace($in, '\\', '\\\\')"/>
</xsl:function>

 
Old June 14th, 2007, 10:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Depends on your processor. Measure it and see. With a good optimizer, functions like that are likely to generate inline code.

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
String manipulation Franco1 Visual Basic 2005 Basics 1 July 16th, 2008 12:53 PM
String manipulation john316 SQL Language 1 October 1st, 2007 04:24 PM
String manipulation YoungLuke C# 4 May 4th, 2007 01:46 AM
String Manipulation twsinc Access 3 February 23rd, 2004 09:57 AM
String Manipulation Ben Access VBA 2 July 8th, 2003 05:53 AM





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