Ashwini
Ashwini

Reputation: 3

using xslt to pass node value as an argument to a javascript function

I am new to this so I don't know whether this approach is the correct way to pass an argument. Please help me correct this or suggest another way to do this. I want to pass a node value from the xslt to a javascript function.

This is my XML file :

   <?xml version="1.0"?>
   <?xml-stylesheet type="text/xsl" href="sample.xsl"?>
   <One>
       <Two>
           HelloWorld
       </Two>
   </One>

This is the xslt file :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:variable name="url" select="One/Two"/>
<xsl:template match="/">
<html>
    <script>
        function Myclick(vara)
        {
            alert(vara);            
        }
    </script>
    <xsl:for-each select="One">
        <a>
            <xsl:attribute name="href">
                http://www.google.com
            </xsl:attribute>
            <xsl:attribute name="onClick">                      
                alert($url);
                Myclick($url);
            </xsl:attribute>            
            <xsl:value-of select="Two"/>
        </a>
    </xsl:for-each>
</html>
</xsl:template>

Upvotes: 0

Views: 2179

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

Your XSLT code is not calling any Javascript function; it is merely generating HTML.

Do you know what HTML you want to generate? If so, tell us, and we can help you generate it. If not, you shouldn't be trying to write XSLT code - never start writing a program (in any language) until you know what output you want it to produce.

Upvotes: 1

AMember
AMember

Reputation: 3057

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
    <xsl:variable name="url" select="One/Two"/>
    <xsl:template match="/">
        <html>
            <head>
                <script>
                    function Myclick(a)
                    {
                        var href = a.getAttribute('href');
                        alert(href);
                        location.href= href;
                    }
                </script>
            </head>
            <body>
                <xsl:for-each select="One">
                    <a>
                        <xsl:attribute name="href">
                            http://www.google.com
                        </xsl:attribute>
                        <xsl:attribute name="onClick">
                            javascript:Myclick(this); return false;
                        </xsl:attribute>
                        <xsl:value-of select="Two"/>
                    </a>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Related Questions