?
Solved

xml/xsl/javascript: problem calling javascript function with onclick in xsl

Posted on 2004-09-01
9
Medium Priority
?
910 Views
Last Modified: 2012-05-05
Hi, does anybody see the error here..? IE says 'object expected...'
The function 'a' is defined on row 8 and called by onclick on row 22. If I replace 'onclick="a()"' with 'onclick="alert()"', it works. And if I write the same function definition and call it the same way in an ordinary html-file it works just fine.
 
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 <xsl:template match="header">
 
 <html>
    <head>
    <script language="javascript"> function a(b) {alert();} </script>
    <title><xsl:value-of select="child::title"/></title>
    </head>
    <body>
       <xsl:apply-templates select="artists"/>
    </body>
 </html>
 </xsl:template>
 
 <xsl:template match="artists">
    <xsl:apply-templates/>
 </xsl:template>
 
 <xsl:template match="artist">
       <div id="{child::name}"><img src="plus_2.gif" height="8" width="8" border="1" onclick="a(2)"></img><xsl:value-of select="child::name"/>
    <xsl:apply-templates select="album"/></div>
 </xsl:template>
 
 <xsl:template match="album">
    <div id="{child::albumtitle}">...<xsl:value-of select="child::albumtitle"/>
    <xsl:for-each select="child::song">    
       <div id="{id}">......<xsl:value-of select="title"/></div>
    </xsl:for-each>  </div>    
 </xsl:template>
 
 </xsl:stylesheet>
 
   

 
0
Comment
Question by:kungkalle
[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
  • 5
  • 3
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 11950152
Should:

    <script language="javascript"> function a(b) {alert();} </script>

be:

    <script language="javascript"> function a(b) {alert(b);} </script>
0
 
LVL 4

Expert Comment

by:chipple
ID: 11950201
Works fine here.
What's the resulting HTML? What engine are you using to process XSLT? (I tested with MSXML.)

Just a guess, but one of the following may fix your problem. Here all work the same.

<script language="javascript"><xsl:text> function a(b) {alert();} </xsl:text></script>

or

<script language="javascript"><![CDATA[ function a(b) {alert();} ]]></script>

Good luck!
0
 

Author Comment

by:kungkalle
ID: 11950210
To TimYates

No, I've already tried that and it doesn't work. The function 'a(t)' is not the one I'm planning to use, it's just the simplest possible to try to find out what's wrong with the function call.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kungkalle
ID: 11950275
To chipple

How do I find out what engine I'm using. Perhaps I forgot to mention it but I'm very new to xml/xsl... This is kind of my first test page.

The two alternatives you suggested did not work.

Thanks
0
 

Author Comment

by:kungkalle
ID: 11950304
For sake of clarity, this is the xml-file I'm using and how I use it together with the xsl-file


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="mp3.xsl"?>
<mp3files>
      <title>Testa xml-xsl</title>
<artists>
      <artist>
            <name id="art_1">Artist 1</name>
            <album>
                  <albumtitle>Album 1</albumtitle>
                  <song>
                        <title>Song 1</title>
                        <id>Artist 1\Album 1\Song 1</id>
                  </song>

                  <song>
                        <title>Song 2</title>
                        <id>Artist 1\Album 1\Song 2</id>
                  </song>            
            </album>
            <album>
                  <albumtitle>Album 2</albumtitle>
                  <song>
                        <title>Song 1</title>
                        <id>Artist 1\Album 2\Song 1</id>
                  </song>

                  <song>
                        <title>Song 2</title>
                        <id>Artist 1\Album 2\Song 2</id>
                  </song>            
            </album>
      </artist>
</artists>
</mp3files>
0
 
LVL 4

Expert Comment

by:chipple
ID: 11950324
How are you viewing the result? Are you accessing your XML file directly in a Web browser? Or do you have server script doing the XML+XSLT=HTML conversion?
0
 

Author Comment

by:kungkalle
ID: 11950330
I view it directly in a web browser
0
 
LVL 4

Accepted Solution

by:
chipple earned 375 total points
ID: 11950345
Your XML helped indeed. Changing
<xsl:template match="header">
to
<xsl:template match="mp3files">
should help.
0
 

Author Comment

by:kungkalle
ID: 11951136
Rewrote both xml and xsl file and then all of a sudden it worked. Have no idea why... Chipple gets the points for trying though.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

741 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