Solved

XSLT - How to build a <a onclick='somefunction(val, val)'> ??  This should be simple

Posted on 2008-11-01
4
1,561 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:rodmjay
  • 2
4 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 250 total points
ID: 22856070
close
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="offer">
    <a onclick="MyWindow=window.open('{terms/@url}', 'offer {count(preceding-sibling::offer) + 1}', 500, 500); return false;">
        See offer Terms and Conditions
    </a>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 250 total points
ID: 22859225
That's interesting Gertone, I didn't realise that you could use XSLT directly within attribute strings like that. I have always used the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="offer">
    <a>
        <xsl:attribute name="onclick">
            <xsl:value-of select="concat('MyWindow=window.open(\'', terms/@url, '\', \'offer ', count(preceding-sibling::offer) + 1, '\', 500, 500); return false;')" />
        </xsl:attribute>
        See offer Terms and Conditions
    </a>
</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22860690
@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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question