Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scripting in XSL

Status
Not open for further replies.

Aleena

Programmer
Oct 3, 1999
27
US
I am trying to figure out how I can add scripting code to an xsl document. I have been scouring the net tonight and coming up quite emptyhanded. Some of the sources I have read seemed to think scripting in xsl was incompatiable or unreliable. I need to be able to incorporate some functions into my xsl document for a project I am doing at school :(

Any helpful pointers or resources someone might be able to recommend? I don't care if it is VBScript, javascript, asp, whatever. Desperation is sinking in... I need to know how to include the script in my xsl document and also how to call a function from said script in the body.

Thank you in advance for any help you might be able to offer :)
 
Hai Aleena!
Adding scripts to xsl is not a big deal...you don't have to sink in desperation for such small challenges...
Anyway...let me see how I can help you...
This is a small xml file with some data..I will call it..
Script.xml...

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;script.xsl&quot;?>
<college>
<computerscience>
<Name>Ravi</Name>
<ScholarNo>101</ScholarNo>
<Grade>Top</Grade>
<GirlFriend>Aishwarya</GirlFriend>
</computerscience>
<computerscience>
<Name>Bill Gates</Name>
<ScholarNo>102</ScholarNo>
<Grade>Okay</Grade>
<GirlFriend>Pamela</GirlFriend>
</computerscience>
<computerscience>
<Name>Geroge Bush</Name>
<ScholarNo>103</ScholarNo>
<Grade>Reasonable</Grade>
<GirlFriend>Barbie</GirlFriend>
</computerscience>
</college>

As you can see I have given a call to a stylesheet called &quot;script.xsl&quot;....go through this file also now.....

<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;<xsl:template match=&quot;/&quot;>
<html>
<script language=&quot;javascript&quot;>
function beep()
{
alert(&quot;Hai Aleena!!You can use any script thru xsl&quot;)
}
</script>
<body>
<table border=&quot;0&quot; bgcolor=&quot;oldlace&quot; text=&quot;blue&quot;>
<caption>Batch of 2001</caption>
<tr>
<th>Name</th>
<th>Scholar No:</th>
<th>Grade</th>
<th>GirlFriend</th>
</tr>
<xsl:for-each select=&quot;college/computerscience&quot;>
<tr>
<td><xsl:value-of select=&quot;Name&quot;/></td>
<td><xsl:value-of select=&quot;ScholarNo&quot;/></td>
<td><xsl:value-of select=&quot;Grade&quot;/></td>
<td><xsl:value-of select=&quot;GirlFriend&quot;/></td>
</tr>
</xsl:for-each>
<tr><td align='center'><input type='button' value='ClickMe' onclick=&quot;beep()&quot;></input></td></tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

See that is all there needs to be done..you write your own <script></script> tag anywhere and write the functions...however note one thing here...every html tag needs to be closed..so even the <input...> demands a </input>..or else an error will be generated!!!
Hope that answers your SOS....
Happy Programming,
Ravi
 
Hi Aleena,

I guess adding scripting functionality while processing the stylesheet is what you really where trying to accomplish..

You can do this if you use the microsoft xml parser. With it you have the ability to add <msxml:script> tags to the stylesheet.

Lets take the xml of Ravi and let's say you want to combine the Name and Girlfriend tags to a string. (e.g. Ravi loves Aishwarya)

You could do this with the xsl concat() function, but because I need an example I'll use vbscript :
Code:
<?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?>
<xsl:stylesheet version=&quot;1.0&quot;
xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL]
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
xmlns:user='urn:user' exclude-result-prefixes='msxsl'>
<xsl:template match=&quot;/&quot;>
    <html>
    <body>
    <table border=&quot;0&quot; bgcolor=&quot;oldlace&quot; text=&quot;blue&quot;>
    <caption>Batch of 2001</caption>
    <tr>
            <th>Name</th>
            <th>Scholar No:</th>
            <th>Grade</th>
            <th>GirlFriend</th>
          </tr>
          <xsl:for-each select=&quot;college/computerscience&quot;>
          <tr>
            <td colspan=&quot;4&quot;>
               <xsl:value-of select=&quot;user:fctstringconcat3(string(Name),' loves ',string(GirlFriend))&quot;/>              </td>
          </tr>
          </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>

<msxsl:script language=&quot;VBScript&quot; implements-prefix=&quot;user&quot;>
<![CDATA[
Function FctStringConcat3( p_strParm0, p_strParm1, p_strParm2 )
  FctStringConcat3 = p_strParm0 + p_strParm1 + p_strParm2
End Function
]]>
</msxsl:script>
</xsl:stylesheet>
Save the stylesheet as script.xsl and open the xml in IE 6 or IE 5.5 (with IE 5.5 you must at least have msxml 3.0 installed in replace mode.

By the way.... it's my opinion that scripting should always be a last resort. Very, very much can be done with XSLT. Scripting only slows the processing down and make the stylesheet incompatible with other engines.

Good luck!

Jordi Reineman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top