This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes Need Help in working with javascript and XSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need Help in working with javascript and XSL" Watch "Need Help in working with javascript and XSL" New topic
Author

Need Help in working with javascript and XSL

SohJai
Greenhorn

Joined: Aug 01, 2001
Posts: 2
Hi,
I have a couple of questions:
1) How do I use script in XSL to work in the HTML it calls it from?
Below is what I tried to do to test things out but it doesn't work, no alert box appears.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/TR/WD-xsl'>
<xsl:template match='/'>
<script language="Javascript"><![CDATA[
alert('ASDF')
]]</script>
</xsl:template>
</xsl:stylesheet>

2) How can I do script that actually runs in XSL?
I want to creat a series of checkboxes each with the name cb1-cb*, * is the number of records that is filtered.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/TR/WD-xsl'>
<xsl:template match='/'>
<xsl:for-each select='data/item[date="27/08/2001"]' order-by='+ time'>
<xsl:element name='input'>
<xsl:attribute name='type'>checkbox</xsl:attribute>
<xsl:attribute name='name'>*****</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I have tried doing <xsl:script></xsl:script> but it returns error telling me I can't use xsl:script, I don't understand, I found many examples online using xsl:script. How else can I name my checkboxes in order?

Thanks in Advance!
SohJai
[This message has been edited by SohJai (edited August 01, 2001).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
First, your namespace 'http://www.w3.org/TR/WD-xsl' refers to outdated version of MSXML XSLT processor - proprietary Microsoft's version of XSLT. It would be better to use 'http://www.w3.org/1999/XSL/Transform'
Next, to see alert box appear, you need to include some HTML tags, to have a valid HTML page.
So your whole XSLT will be:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<script language="JavaScript"><![CDATA[
alert('ASDF')
]]></script>
<body/>
</head>
</html>
</xsl:template>
</xsl:stylesheet>

2. <xsl:script> isn't a standard XSL element, if I am not mistaken, it' a Microsoft extension. The same about "order-by". All what you need is position() function - it returns a position of the current element in a node-set:
<xsl:template match="/">
<html>
<body>
<form>
<xsl:for-each select='data/item[date="27/08/2001"]'>
<xsl:sort select="time"/>
<input type="checkbox" name="{concat ('cb', position())}"/><br/>
</xsl:for-each>
</form>
</body>
</html>
</xsl:template>

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
 
jQuery in Action, 2nd edition
 
subject: Need Help in working with javascript and XSL