Solved

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

Posted on 2004-09-01
9
896 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

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 125 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

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!

Question has a verified solution.

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

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

724 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