rodmjay
asked on
XSLT - How to build a <a onclick='somefunction(val, val)'> ?? This should be simple
Im trying to build the following
<a onclick="MyWindow=window.o pen('{url} ', 'offer {element #}', 500, 500); return false;">
See offer Terms and Conditions
</a>
the current xml element looks like this
<offers>
<offer> <-- This is the current element
<terms url="http://someurl"/>
</offer>
</offers>
the {element #} refers to the offer in offers, like the index of that offer
<a onclick="MyWindow=window.o
See offer Terms and Conditions
</a>
the current xml element looks like this
<offers>
<offer> <-- This is the current element
<terms url="http://someurl"/>
</offer>
</offers>
the {element #} refers to the offer in offers, like the index of that offer
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yep, using xsl:attribute would be the next option,
for readability, I would then do this
<a>
<xsl:attribute name="onclick">
<xsl:text>MyWindow=window.
<xsl:value-of select="terms/@url"/>
<xsl:text>','offer </xsl:text>
<xsl:value-of select="count(preceding-si
<xsl:text>', 500, 500); return false;</xsl:text>
</xsl:attribute>
See offer Terms and Conditions
</a>
That saves me from escaping all the single quotes
(your example is a bit broken in that aspect)
Sometimes I find it easier to simply show the attribute directly.
In order to make sure that inside the attribute, XPath expressions will be evaluated,
you will have to put them inside {} (these are called "attribute value templates"
There are a number of other places in XSLT where AVTs can be used
(eg. when you want to make element and attribute names dynamic:
<xsl:element name="myElement{position()
which generates <myElem1> when position() = 1)
I would mainly use AVTs when I don't want the verboseness of xsl:attribute
or when often when the AVT adds to the readibility, as in this case with the javascript
cheers
Geert