• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 858
  • Last Modified:

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.




0
sybe
Asked:
sybe
  • 3
  • 3
  • 2
  • +1
1 Solution
 
MMeijerCommented:
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)">
0
 
sybeAuthor Commented:
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.
0
 
topcat_ukCommented:
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>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sybeAuthor Commented:
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  
0
 
topcat_ukCommented:
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
0
 
sybeAuthor Commented:
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.

0
 
sparkplugCommented:
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<
0
 
sparkplugCommented:
.. the function should be:

function escapeQuotes(s)
{
     return s.replace(/\'/g, '\\\'');
}

as this deals with more than one single quote.

>S'Plug<
0
 
sparkplugCommented:
B?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now