Link to home
Start Free TrialLog in
Avatar of rodmjay
rodmjayFlag for United States of America

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.open('{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
SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@numbercruncher,
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.open('</xsl:text>
                    <xsl:value-of select="terms/@url"/>
                    <xsl:text>','offer </xsl:text>
                    <xsl:value-of select="count(preceding-sibling::offer) + 1"/>
                    <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