Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 06:31 AM
Registered User
 
Join Date: Dec 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use Javascript in XSL

Dear Sir

I want to know how to use the Javascript in the XSL document.

I am using like below but its not working.

<script language="javascript">
<xsl:for-each select="test-results/test-suite/results/test-suite/results/test-suite">
var test;
test=<xsl:value-of select="./@name"/>;
document.write(valueof(test) + "<br/>") ;
</xsl:for-each>
</script>

Please help me regarding this.
Regards
Umesha Y.K

 
Old January 2nd, 2004, 12:01 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you are using Microsoft parser I suggest going to msdn.com and looking for Microsoft xml core services. The downloadable help file (otherwise known as sdk) has lots of examples on using JavaScript.

Joe (MVP - xml)
 
Old January 10th, 2004, 10:21 AM
Authorized User
 
Join Date: Jan 2004
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

An example:

The XML Document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
   </cd>
   <cd>
     <title>Greatest Hits</title>
     <artist>Dolly Parton</artist>
     <country>USA</country>
     <company>RCA</company>
     <price>9.90</price>
     <year>1982</year>
    </cd>
</catalog>


The following is the XSLT Document:


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
  <html>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th align="left">Title</th>
        <th align="left">Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="artist" /></td>
      </tr>
      </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template></xsl:stylesheet>

JavaScript code:

<html>
<body><script type="text/javascript">// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cdcatalog.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cdcatalog.xsl")

// Transform
document.write(xml.transformNode(xsl))</script>

</body>
</html>

The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the XML document into memory. The second block of code creates another instance of the parser and loads the XSL document into memory. The last line of code transforms the XML document using the XSL document, and writes the result to the XHTML document.

-Nikolas



 
Old January 16th, 2004, 11:33 AM
Registered User
 
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have you considered a 'scripts' template which has a CDATA section containing your javaScript? You could then call the template to dumpt the CDATA in the required place in your transform.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL & JavaScript Ryan Moore XSLT 1 October 9th, 2007 01:36 PM
xsl javascript problem smi13y XSLT 1 December 26th, 2006 07:27 PM
XSL maybe javascript. suri_1811 XSLT 1 November 3rd, 2006 12:20 PM
External JavaScript with XSL NEO1976 XSLT 2 September 5th, 2006 07:19 AM
Javascript with XSL drdexter33 XSLT 2 January 19th, 2006 01:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.