Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.

757 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

18 Experts available now in Live!

Get 1:1 Help Now