views:

53

answers:

1

Basically, I'm looking to get 'URLValue' if the particular <a> is clicked and pass it on to another method. There are several other <a> elements with class="LinkClass" and I have written a JQuery to get only the clicked element value. Below is a working JQuery to do just this, it references the XSL.

 $("a.LinkClass").live("click", function() {
        var URL = $(this).attr("href");  
        //Now call another method passing this value
    });

However, can I use the value directly through XSL, triggering a function call on event click for the link?

XSL below:

 <a class="LinkClass">
      <xsl:attribute name="href">
        <xsl:value-of select="URLValue"/>
      </xsl:attribute>
  </a>
+1  A: 

When this XML document is open on browser:

<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <title>Test XSLT javascript injektion</title>
    </head>
    <body>
        <h2>Test XSLT javascript injektion</h2>
        <ul>
            <li><a href="http://www.google.com"&gt;Google&lt;/a&gt;&lt;/li&gt;
            <li><a href="http://www.stackoverflow.com"&gt;Stack Overflow</a></li>
        </ul>
    </body>
</html>

And this stylesheet as "test.xsl":

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"&gt;
    <xsl:output method="xml" omit-xml-declaration="yes"
     doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/&gt;
    <xsl:template match="processing-instruction()" priority="1"/>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="xhtml:a/node()[1]">
        <xsl:attribute name="onclick">
            <xsl:value-of select='concat("alert(&apos;",..,"&apos;)")'/>
        </xsl:attribute>
        <xsl:call-template name="identity"/>
    </xsl:template>
</xsl:stylesheet>

Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <title>Test XSLT javascript injektion</title>
    </head>
    <body>
        <h2>Test XSLT javascript injektion</h2>
        <ul>
            <li>
                <a href="http://www.google.com" onclick="alert('Google')">Google</a>
            </li>
            <li>
                <a href="http://www.stackoverflow.com" onclick="alert('Stack Overflow')">Stack Overflow</a>
            </li>
        </ul>
    </body>
</html>

And alerts works on click.

Alejandro
thank you. It worked!
Amy
@Amy: You are wellcome!
Alejandro
actually the XSL is not working :( the output page is working let me also try again
Amy
@Amy: If you are sending this trought the wire, take care of MIME type.
Alejandro
is there any other way to get this working?
Amy
<xsl:template match="Node" mode="URL"> <a class="URL1"> <xsl:if test="DisplayTargetTypeID='102'"> <xsl:attribute name="target">_blank</xsl:attribute> </xsl:if> <xsl:attribute name="href"> <xsl:value-of disable-output-escaping="yes" select="URLvalue"/> </xsl:attribute> <xsl:attribute name="onclick"> <xsl:value-of select='concat("ajaxws.method1('",..,"')")'/> </xsl:attribute> </a> </xsl:template>
Amy
@Amy: I don't know why you need DOE, but this is more compact: `<xsl:variable name="vEncodeURL"><xsl:value-of disable-output-escaping="yes" select="URLvalue"/></xsl:variable>` `<a class="URL1" target="{concat(substring('_blank',1 DIV DisplayTargetTypeID = '102'),substring('_top',1 DIV DisplayTargetTypeID != '102'))}" href="{$vEncodeURL}" onclick="ajaxws.method1('{..}')" />`
Alejandro