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

Re: select="substring-before($str,"'")" !!!

Subject: Re: select="substring-before($str,"'")" !!!
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 16 Jan 2001 13:51:54 +0000
xsl value of select substring
Hi Phil,

Within an attribute value, &apos; gives you an apostrophe and &quot;
gives you a double-quote.  The XPath interpreter sees the value of the
attribute after all the &apos;es have been exchanged for 's and the
&quot;s for "s.  So:

  substring-before($str, "'")

could be written:

  substring-before($str, &quot;&apos;&quot;)

You only *have* to escape the quotes or apostrophes that you use
around the attribute value, so if you use double quotes, then do
something like:

  select="substring-before($str, &quot;'&quot;)"

or

  select='substring-before($str, "&apos;")'

Just a quick other comment: Did you know that you can use the 'select'
attribute on xsl:variable, xsl:param and xsl:with-param rather than
use the content and xsl:value-of? For example, your template:

>   <xsl:template name="escape-string-for-javascript">
>     <xsl:param name="str"/>
>     <xsl:choose>
>       <xsl:when test="contains($str,"'")">
>         <xsl:variable name="before-first-apostrophe"><xsl:value-of select="substring-before($str,"'")"/></xsl:variable>
>         <xsl:variable name="after-first-apostrophe"><xsl:value-of select="substring-after($str,"'")"/></xsl:variable>
>         <xsl:value-of select="$before-first-apostrophe"/>\'<xsl:call-template name="escape-string-for-javascript"><xsl:with-param name="str"><xsl:value-of
> select="$after-first-apostrophe"></xsl:with-param></xsl:call-template>
>       </xsl:when>
>       <xsl:otherwise>
>         <xsl:value-of select="$str">
>       </xsl:otherwise>
>     </xsl:choose>
>   </xsl:template>

could be written:

   <xsl:template name="escape-string-for-javascript">
     <xsl:param name="str"/>
     <xsl:choose>
       <xsl:when test='contains($str,"&apos;")'>
         <xsl:variable name="before-first-apostrophe"
                       select='substring-before($str,"&apos;")'/>
         <xsl:variable name="after-first-apostrophe"
                       select='substring-after($str,"&apos;")'/>
         <xsl:value-of select="$before-first-apostrophe"/>\'<xsl:text/>
         <xsl:call-template name="escape-string-for-javascript">
            <xsl:with-param name="str" select="$after-first-apostrophe" />
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="$str">
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>

and called like:

document.writeln('<xsl:call-template name="escape-string-for-javascript">
                     <xsl:with-param name="str" select="name"/>
                  </xsl:call-template>');

Using the 'select' attribute is usually better, if you can get away
with it, because it doesn't build a result tree fragment as the
variable value, but uses a string, number, boolean or node-set.  That
generally means it's more efficient and doesn't react in strange and
confusing ways to boolean tests (for example).

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 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.