[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 928
  • Last Modified:

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

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
kungkalle
Asked:
kungkalle
  • 5
  • 3
1 Solution
 
TimYatesCommented:
Should:

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

be:

    <script language="javascript"> function a(b) {alert(b);} </script>
0
 
chippleCommented:
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
 
kungkalleAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kungkalleAuthor Commented:
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
 
kungkalleAuthor Commented:
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
 
chippleCommented:
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
 
kungkalleAuthor Commented:
I view it directly in a web browser
0
 
chippleCommented:
Your XML helped indeed. Changing
<xsl:template match="header">
to
<xsl:template match="mp3files">
should help.
0
 
kungkalleAuthor Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now