Re: [xsl] Javascript in XSLT

Subject: Re: [xsl] Javascript in XSLT
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Mon, 24 Jun 2002 09:36:41 -0400
[Joel Konkle-Parker]
>
> I've got the following event handlers in my XSLT-to-html stylesheet:
>
> onmouseover="showSubMenu('menuGamesDrop')"
> onmouseout="hideSubMenu('menuGamesDrop')"
>
> The Games section, however, gets generated on the fly from another
> file, so I actually have the following:
>
> onmouseover="{concat('showMenu&#x0028;&apos;menu', category_name,
> 'Drop&apos;&#x0029;')}"
> onmouseout="{concat('hideMenu&#x0028;&apos;menu', category_name,
> 'Drop&apos;&#x0029;')}"
>
> Where (if they show up) ', (, and ) are all escaped when they're
> needed in the event handlers. When I try this though, I get errors in
> Saxon and IE javascript errors. The exact Saxon message is:
>
> Error at div on line 34 of
> http://www.ballsome.com/test/stylesheets/home.xsl:
>   Error in expression concat('showMenu('menu', category_name,
> 'Drop')'): expecte
> d ")", found "<name>"
>
> Looks like it's replacing the entity references before it processes
> the XSLT, so the single quotes are messing the concat function up. Is
> there any way to fix this? Will it accept alternating single and
> double quotes or something like that?
>

Of course the enties and character references are being replaced first,
that's the parser's job.  Just simplify the expressions to minimize the
chance of confusing yourself, like this:

<div onmouseover="showMenu('menu',{category_name},'Drop')"/>

This outputs (where "cat_1" is the contents of the category_name element):

<div onmouseover="showMenu('menu',cat_1,'Drop')"/>

This is easy to read and debug.  Of course, I'm assuming that
"category_name" is an element that contains character data with the name you
want, and from your previous post that doesn't seem quite right, but you get
the idea.

However, there is one qualification.  For this to work, you have to start
the attribute value with the same kind of quote (single or double) as the
xslt processor uses for outputting attributes.  I found that the above works
with msxml3, saxon, and xalan.  If you reverse the quotes like this, you end
up with escaped quotes in the javascript:

<div onmouseover='showMenu("menu",{category_name},"Drop")'/>

To be completely portable, you would have to escape your quotes, which of
course is less readable:

<div
onmouseover='showMenu(&apos;menu&apos;,{category_name},&apos;Drop&apos;)'/>

Cheers,

Tom P




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread