Sam
Sam

Reputation: 5250

Unable to use xsl variable in Javascript

I am using an embedded stylesheet into xml document Referring the link. My intension is getting the filename from xml tag through XSLT and validating it with the help of Javascript. I am trying to pass an xsl variable value to a javascript function. My alert is not working. I am sure the browser runs my Javascript.

But I am getting the syntax error error image

My XML code

<?xml version="1.0" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="#id(xyz)"?>
<file>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="xyz">

<xsl:template match="/">
<xsl:variable name="fName" select="filename" />
<xsl:text><xsl:value-of select="$fName"/></xsl:text>
 <html>
    <head>
      <script type="text/javascript">

      var fileName = "<xsl:value-of select="$fName"/>"; 
      alert(fileName);
      </script>
    </head>
    <body>     

    </body>
 </html>
</xsl:template>
</xsl:stylesheet>

<filename>10052015</filename>
</file>

Upvotes: 0

Views: 239

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

As far as I remember, Internet Explorer never supported referencing embedded stylesheets.

With Firefox and Chrome http://home.arcor.de/martin.honnen/xslt/test2015091801.xml works for me, its source code is

<?xml version="1.0" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="#xyz"?>
<!DOCTYPE file [
  <!ATTLIST xsl:stylesheet
     id ID #REQUIRED>
]>
<file>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="xyz" version="1.0">

<xsl:template match="/">
 <xsl:variable name="fName" select="file/filename" />
 <html>
    <head>
      <script type="text/javascript">

      var fileName = "<xsl:value-of select="$fName"/>"; 
      alert(fileName);
      </script>
    </head>
    <body>     
      <h1>Test</h1>
    </body>
 </html>
</xsl:template>
</xsl:stylesheet>

<filename>10052015</filename>
</file>

Upvotes: 1

Related Questions