Solved

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

Posted on 2008-11-01
4
1,562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT XML 4 32
Grunt Copy file to another destination. 1 76
xml files 7 53
Passing client data from form to server 5 16
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

735 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