my dog learned polymorphism*
The moose likes XML and Related Technologies and the fly likes combining JavaScript and XSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "combining JavaScript and XSL" Watch "combining JavaScript and XSL" New topic
Author

combining JavaScript and XSL

Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
I am trying to use XSL to convert my XML files to DHTML so that I can use it in both Netscape and IE. What I'd like to do is create two different stylesheets and have a JSP that chooses the one that's appropriate for the user browser.
Now here is the catch.
My DHML page would include somthing like this
<TD><A HREF="#" onclick=clicker("menu 1")>Item 1</A>
This line would need to be generated from the following XML code
<menu name="menu 1">
<menu_item name="item 1" url="url1">
<menu_item name="item 2" url="url2">
</menu>
The problem I have is that I cannot combine xls:value-of and embed that value into my onclick attribute.
Any ideas?
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Hi Aleksey!
To insert XML data in attribute values, you can use so-called "attribute-value template":
{XPath expression}
which means the same as
<xsl:value-of select="XPath expression">
In your case it will be:
<xsl:for-each select="menu/menu_item">
<A HREF="#" onclick="clicker('{../@name}')">
<xsl:value-of select="./@name"/></A>
</xsl:for-each>

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
Thanks.
I also found another way by using <xsl:call-template> method.
Your method though is easier.
Another question:
how would you insert the value into an javascript expression
so you would get the code that looks like:
<script language = "javascript">
document.write("item 1")
document.write("item 2")
....
</script>
item 1, itwm2 and so on are comming from xml file such as
<xml>
<menu name="item 1"/>
<menu name="item 2"/>
</xml>

[This message has been edited by Aleksey Matiychenko (edited June 27, 2001).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
If you want to hardcode values in your XSLT document, you can use old good <xsl:value> element:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<script language = "javascript">
<xsl:for-each select="menu/menu_item">
document.write("<xsl:value-of select="./@name"/>");
</xsl:for-each>
</script>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

If you want to make your JavaScript and XSLT communicate at run-time, then as far as I understand, you have two options:
1) call your JavaScript function and pass some attribute/element value as a parameter. This can be done via extension functions, if your XSLT processor supports them. Xalan�s implementation is described here (for example).
2) If you have too many values to pass, then it may be easier to make your JavaScript itself read XML file, something like
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("myFile.xml");
But this is a client-side = proprietary (in this case Micro$oft proprietary) solution.
Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
thanks. the solution was actually very easy. value-of worked great in constructing javascript.
Thanks for your help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: combining JavaScript and XSL
 
Similar Threads
XSL: test for first and last occurence of template match
How do I recover the String
JQuery replace div
How do I apply a template based on a node value
split an xml string using xpath?