Link to home
Start Free TrialLog in
Avatar of sybe
sybe

asked on

problems with apostroph in xml/xsl to javascript.

I have been trying to solve this in various ways but can not find a working solution.

Data in an XML file contains an apostroph (within a string).
The xml is parsed using XSL, this works fine to HTML
The problem occurs when i want the data to appear as parameter in a javascript function call.

more concrete example:

=== XML ===

<data>
    <record>
        <somename>here's an apostroph</somename>
    </record>
</data>

=== XSL (fragment) ===

<input>
    <xsl:attribute name="type">button</xsl:attribute>
    <xsl:attribute name="value">click here</xsl:attribute>
    <xsl:attribute name="onclick">SomeJavascriptFunctionWithParameter('<xsl:value-of select="somename"/>')</xsl:attribute>                    
</input>


=== result would be ====

<input type="button" value="click here" onclick="SomeJavascriptFunctionWithParameter('here's an apostroph')">

this of course gives a javascript error.
I have been unable to find a way to solve this. I tried replacing the apostroph with "\'", but i could not do that with XSL.




Avatar of MMeijer
MMeijer

put the xsl with aphostroph in an element on the page and give the function it's value as parameter.

<input type="hidden" id="somevalue" value="{somename}" />
<input type="button" value="click here" onclick="SomeJavascriptFunctionWithParameter(document.getElementById('somevalue').value)">
Avatar of sybe

ASKER

sounds like it could work for an apostroph, but it shifts the problem to other characters, like double-quote(").

i'll keep this one in mind, but i prefer to solve it in XSL, and not in html/javascript.
Hi sybe,

I have found changing ' to \' works ok in js.
This is not my example, it appeared in another group I belong to.

This template is used to replace '

<xsl:template name="escape-apos">
   <xsl:param name="string" />
   <!-- create an $apos variable to make it easier to refer to -->
   <xsl:variable name="apos" select='"&apos;"'" />
   <xsl:choose>
      <!-- if the string contains an apostrophe... -->
      <xsl:when test='contains($string, $apos)' />
         <!-- ... give the value before the apostrophe... -->
         <xsl:value-of select="substring-before($string, $apos)" />
         <!-- ... the escaped apostrophe ... -->
         <xsl:text>\'</xsl:text>
         <!-- ... and the result of applying the template to the
                  string after the apostrophe -->
         <xsl:call-template name="escape-apos">
            <xsl:with-param name="string"
                            select="substring-after($string, $apos)" />
         </xsl:call-template>
      </xsl:when>
      <!-- otherwise... -->
      <xsl:otherwise>
         <!-- ... just give the value of the string -->
         <xsl:value-of select="$string" />
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>
Avatar of sybe

ASKER

when i copy/paste this i get an error

<xsl:variable name="apos" select='"&apos;"'" />


Required white space was missing. Error processing resource
Line 18, Position 46  
Hi sybe,

As I stated earlier, it is not my example so here is the url for the post.

http://www.biglist.com/lists/xsl-list/archives/200102/msg01607.html

Good luck
Avatar of sybe

ASKER

My problem is no longer urgent, as I have chosen to use a workaround.

But I am still interested in a working solution. I have found several proposals on internet but havn't gotten anything to work.

My work-around is that i only pass the id to the javascript function, and that the javascript function opens the xml file and reads the values from the XML in stead of getting them as parameters.

Sybe,

You could use some script in your XSL as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mydomain/scripts">
     <xsl:output  method="html"/>
     <msxsl:script language="javascript" implements-prefix="user">
     <![CDATA[
          function escapeQuotes(s)
          {
               return s.replace('\'', '\\\'');
          }
     ]]>
     </msxsl:script>
     
     <xsl:template match="/">
               <input>
                  <xsl:attribute name="type">button</xsl:attribute>
                  <xsl:attribute name="value">click here</xsl:attribute>
                  <xsl:attribute name="onclick">alert('<xsl:value-of select="user:escapeQuotes(string(data/record/somename))"/>')</xsl:attribute>                    
               </input>
     </xsl:template>
</xsl:stylesheet>

This is however MSXML only.

>S'Plug<
ASKER CERTIFIED SOLUTION
Avatar of sparkplug
sparkplug

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial