xsl-list
[Top] [All Lists]

Re: [xsl] ESCAPE SINGLE QUOTES FOR JAVASCRIPT PARAMETER IN XSLT

2008-06-05 08:46:02
Cleyton Jordan wrote:

<A AR="I Don't Watch It" AID="768564" APID="76158" AC="5"/>

As you can see the value of the @AR attribute has a single quote. This is causing me a lot of problems when I use xslt to call a javascript function passing the parameters.
I was wondering if someone could help me to find a solution for that.

Here is an XSLT 2.0 function that escapes four characters as needed for JavaScript string literals:

  <xsl:function name="my:escape" as="xs:string">
    <xsl:param name="s" as="xs:string"/>
    <xsl:sequence
      select="replace(
                replace(
                  replace(
                    replace($s, &quot;'&quot;, &quot;\\'&quot;),
                    '&quot;',
                    '\\&quot;'
                  ),
                  '\r',
                  '\\r'
                ),
                '\n',
                '\\n'
              )"/>
  </xsl:function>

You can the use it as e.g.
  '<xsl:value-of select="my:escape(@AR)"/>'


--

        Martin Honnen
        http://JavaScript.FAQTs.com/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--