Altova Mailing List Archives

Re: [xsl] Escaping apostrophes for use in javascript, etc. - how to trouble shoot template results?

From: Jeni Tennison <jeni@---------------->
Date: 10/9/2003 6:15:00 AM
Hi Kathy,

> I'm using a stylesheet whose output includes some javascript
> (typical stuff, validation, submit, etc.) run exclusively in IE6
> using msxml4. One of the functions includes a parameter whose text
> can contain apostophes.
> function Anomaly(typeIn,idIn,textIn)
> newWindow =
> 'Anomaly', 'width=650,height=700');
> This passes certain text nodes as parameters, which then get
> included in the querystring to the next form.

Presumably you're generating this JavaScript using XSLT? Can you show
us the XSLT that you're using to generate it?

> Trying to solve this, I included a template written by Jeni Tennison
> (from an online post) to escape apostrophes. In the variable
> name="apos" the select='"&apos;"' -- I don't understand how/if this
> is working. Does xsl look for the actual ' character?

Yes. When an XML document (the XSLT stylesheet in this case) is
parsed, the entity reference &apos; is replaced by an apostrophe and
this value is passed to the application (the XSLT processor in this
case). As far as the XSLT processor is concerned, the value of the
select attribute is "'", which is a legal XPath expression consisting
of a single string literal (delimited by "s) whose value is a string
containing a single apostrophe character.

The template searches for an apostrophe within the string and replaces
all occurrences with the two characters \'.

> And how would one tell if it's working...I know I wouldn't see a
> backslash appear with the apostrophe in the html output
> transformation, but how do I check if this is working?

Um, well, if you're using the template in the way that it's designed
to be used (which is to escape strings that are then output in the
result of the stylesheet), then you should see a backslash appear with
the apostrophe in the source HTML output of the XSLT stylesheet. You
say that you're using Xselerator? In the Output Window, choose the
"View output as text" option and look at the JavaScript.

I'm not actually sure what problem you're encountering. Are you
getting a JavaScript error when you view the page? What does it say?
The usual advice when generating HTML applies: first create an HTML
page that works, containing the JavaScript that works, and then create
that HTML page using XSLT. That way you can separate problems with the
JavaScript from problems with the XSLT.



Jeni Tennison

 XSL-List info and archive:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.