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:at tribute>
<xsl:attribute name="value">click here</xsl:attribute>
<xsl:attribute name="onclick">SomeJavascr iptFunctio nWithParam eter('<xsl :value-of select="somename"/>')</xsl :attribute >
</input>
=== result would be ====
<input type="button" value="click here" onclick="SomeJavascriptFun ctionWithP arameter(' 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.
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:at
<xsl:attribute name="value">click here</xsl:attribute>
<xsl:attribute name="onclick">SomeJavascr
</input>
=== result would be ====
<input type="button" value="click here" onclick="SomeJavascriptFun
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.
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.
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='"'"'" />
<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($s tring, $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>
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='"'"'" />
<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($
<!-- ... 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($s
</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>
ASKER
when i copy/paste this i get an error
<xsl:variable name="apos" select='"'"'" />
Required white space was missing. Error processing resource
Line 18, Position 46
<xsl:variable name="apos" select='"'"'" />
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
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
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.
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-m icrosoft-c om: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:at tribute>
<xsl:attribute name="value">click here</xsl:attribute>
<xsl:attribute name="onclick">alert('<xsl :value-of select="user:escapeQuotes( string(dat a/record/s omename))" />')</xsl: attribute>
</input>
</xsl:template>
</xsl:stylesheet>
This is however MSXML only.
>S'Plug<
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-m
<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:at
<xsl:attribute name="value">click here</xsl:attribute>
<xsl:attribute name="onclick">alert('<xsl
</input>
</xsl:template>
</xsl:stylesheet>
This is however MSXML only.
>S'Plug<
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
B?
<input type="hidden" id="somevalue" value="{somename}" />
<input type="button" value="click here" onclick="SomeJavascriptFun