[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Escaping quotes in XPath expressions

Subject: Re: Escaping quotes in XPath expressions
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 15 Nov 2003 14:53:06 +0100
xpath escape
> Can anyone please tell me how I can escape a single-quote in an XPath
expression
> such as the one below?
>
> <xsl:value-of select="translate(normalize-space(.), 'x', 'y')"/>
>
> If I use 'x' and 'y' as the search and replace arguments, everything works
well. But I
> need to search for single-quotes and replace them with backslash +
single-quote. In
> other words, my XPath expression would look something like this:
>
> translate(normalize-space(.), ''', '\'')

translate() can only translate a character into another character or to
nothing. It is not possible using translate() to translate a character into
two or more characters.

For such tasks it is convenient to use the "str-map" template from FXSL.


This transformation:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:escape="my:escape"
exclude-result-prefixes="xsl escape"
>
   <xsl:import href="str-map.xsl"/>

   <xsl:output method="text"/>
   <xsl:variable name="vQ">'</xsl:variable>

 <xsl:template match="/">
     <xsl:variable name="vEscape" select="document('')/*/escape:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vEscape"/>
       <xsl:with-param name="pStr">a'b'c'd</xsl:with-param>
     </xsl:call-template>
   </xsl:template>

   <escape:escape/>
    <xsl:template name="excape" match="escape:*">
      <xsl:param name="arg1"/>

      <xsl:if test="$arg1 = $vQ">\</xsl:if>
      <xsl:value-of select="$arg1"/>
    </xsl:template>

</xsl:stylesheet>

when performed (on any source.xml -- not used) produces the wanted result:

a\'b\'c\'d


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.