Solved

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

Posted on 2008-11-01
4
1,559 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now