[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: select="substring-before($str,"'")" !!!
Hi Phil, Within an attribute value, ' gives you an apostrophe and " gives you a double-quote. The XPath interpreter sees the value of the attribute after all the 'es have been exchanged for 's and the "s for "s. So: substring-before($str, "'") could be written: substring-before($str, "'") 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, "'")" or select='substring-before($str, "'")' 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,"'")'> <xsl:variable name="before-first-apostrophe" select='substring-before($str,"'")'/> <xsl:variable name="after-first-apostrophe" select='substring-after($str,"'")'/> <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
|
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
|