?
Solved

problems with apostroph in xml/xsl to javascript.

Posted on 2003-03-18
9
Medium Priority
?
854 Views
Last Modified: 2013-11-19
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
Comment
Question by:sybe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:MMeijer
ID: 8159675
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
 
LVL 28

Author Comment

by:sybe
ID: 8159759
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
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8159930
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 28

Author Comment

by:sybe
ID: 8160430
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
 
LVL 1

Expert Comment

by:topcat_uk
ID: 8160654
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
 
LVL 28

Author Comment

by:sybe
ID: 8165550
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
 
LVL 9

Expert Comment

by:sparkplug
ID: 8166381
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
 
LVL 9

Accepted Solution

by:
sparkplug earned 375 total points
ID: 8166419
.. the function should be:

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

as this deals with more than one single quote.

>S'Plug<
0
 
LVL 9

Expert Comment

by:sparkplug
ID: 8253222
B?
0

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question