aspose file tools*
The moose likes XML and Related Technologies and the fly likes Passing parameters in XSL to JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Passing parameters in XSL to JavaScript" Watch "Passing parameters in XSL to JavaScript" New topic
Author

Passing parameters in XSL to JavaScript

Jolene Dicks
Greenhorn

Joined: Aug 09, 2001
Posts: 12
I am trying to incorporate a JavaScript function that I found into my JSP and XSL files. The JavaScript basically takes one or more values from a list box and places them to another list box.
I tried placing the JavaScript code in both my JSP and my XSL. Right now I have it in both and I'm not sure where it has to be. In anycase, that doesn't matter right now cause it isn't working anyway.
When I click on the first button, just to copy whatever is selected in the first list box... nothing happens and a 'Error on Page' message appears in the status bar of my browser.
I'm including my code for you to have a look at.... any help would be very appreciated. Thanks.
XSL Stylesheet (Dropdown.xsl)______________________________________
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<script language="JavaScript">
<![CDATA[
function deleteOption(object,index) {
object.options[index] = null;
}
function addOption(object,text,value) {
var defaultSelected = true;
var selected = true;
var optionName = new Option(text, value, defaultSelected, selected)
object.options[object.length] = optionName;
}
function copySelected(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
if (fromObject.options[i].selected)
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options[i].selected)
deleteOption(fromObject,i);
}
}
function copyAll(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
deleteOption(fromObject,i);
}
}
]]>
</script>
<html>
<body>
<table border = "0" width ="50%" cellpadding = "0" cellspacing = "0">
<tr align="left">Choose a telephone number</tr>
<td align="left"></td>
<tr align="left"></tr>
<td><select name="select1" multiple="true" size="8" >
<xsl:for-each select="Results/CustomerView">
<option><xsl:value-of select= "Phone"/></option>
</xsl:for-each>
</select>
</td>
<td>
<input type="button">
<xsl:attribute name="onclick">
<xsl:text>javascript:copySelected('<xsl:value-of select="@Phone"/>')</xsl:text>
</xsl:attribute>
</input>
<p align="left"></p>
<input type="button" value=" remove one " onClick="if (document.images) copySelected(this.form.select2,this.form.select1)"></input>
<p align="left"></p>
<input type="button" value="move all" onClick="if (document.images) copyAll(this.form.select1,this.form.select2)"></input>
<p align="left"></p>
<input type="button" value="remove all" onClick="if (document.images) copyAll(this.form.select2,this.form.select1)"></input>
</td>
<td>
<select name="select2" multiple="true" size="8">
</select>
</td>
</table>
</body>
</html>
</xsl:template>
<xsl:apply_templates/>
</xsl:stylesheet>

JSP__________________________________________________________________
<?xml version = "1.0"?>
<%@ page contentType="text/html;charset=WINDOWS-1252"%>
<script language="JavaScript"><!--
function deleteOption(object,index) {
object.options[index] = null;
}
function addOption(object,text,value) {
var defaultSelected = true;
var selected = true;
var optionName = new Option(text, value, defaultSelected, selected)
object.options[object.length] = optionName;
}
function copySelected(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
if (fromObject.options[i].selected)
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
document.write("Here I am");
for (var i=fromObject.options.length-1;i>-1;i--) {
if (fromObject.options[i].selected)
deleteOption(fromObject,i);
}
}
function copyAll(fromObject,toObject) {
for (var i=0, l=fromObject.options.length;i<l;i++) {
addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
}
for (var i=fromObject.options.length-1;i>-1;i--) {
deleteOption(fromObject,i);
}
}
--></script>
<jsp:useBean class="oracle.jbo.html.databeans.XmlData" id="custQuery" scope="request" >
<%
custQuery.setStylesheet("Dropdown.xsl");
custQuery.setReleaseApplicationResources(false);
custQuery.setDisplayAttributes("Custid,Name");
custQuery.initialize(pageContext,"BC4JTest_BC4JTest_BC4JTestModule.CustomerView");
custQuery.render();
%>
</jsp:useBean>
I've tried several things with the first button (explains why it is different from the other four)... I will take any suggestions or references to examples of code,. etc...
As you can see I'm new to this.. .thanks again.,
Jolene Dicks
Programmer/Analyst
iNet Development, HRDC
(709) 772-0908
jolene.dicks@hrdc-drhc.gc.ca
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I�m not really sure of what you�re trying to do. Javascript in and XSL can only be used for xsl transformations, unless you want the script to appear in the outputted HTML, but then you cannot call it from your xsl. If you need the javascript (or java classes or perl or phyton for that matter) to help you do the transformations, you need to create a namespace in your xsl and define the functions. (You will need the most recent version of Xalan and Big Blue�s Bean Scripting Framework (and rhino.jar if it�s javascript).
Your XSL should look something like this: (uses both javascript (js namespace) and java class (metadados namespace))
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt"
xmlns:js="ext1"
xmlns:metadados="ext2"
extension-element-prefixes="js metadados">
<xsl utput method="xml" indent="yes"/>
<lxslt:component prefix="js" functions="timestamp indic">
<lxslt:script lang="javascript">
function timestamp(date, hour){
return date + ' ' + hour;
}
function indic(n, d){
return d == 0 ? 0 : n/d;
}
function calc2(a, b, c){
d = indic(a, b);
e = indic(b, c);
return d > e ? "CONG" : "BLOK";
}
function calc3(a, b, c){
d = indic(a, b);
e = indic(b, c);
return a > e ? "CONG" : "BLOK";
}
function calc4(a, b, c){
d = indic(a, b);
e = indic(b, c);
return e > a ? "CONG" : "BLOK";
}
function calc1(a, b, c){
d = indic(a, b);
e = indic(b, c);
return e > a ? "CONG" : "BLOK";
}
</lxslt:script>
</lxslt:component>
<lxslt:component prefix="metadados" elements="insert update delete">
<lxslt:script lang="javaclass" src="transform.MetadadosUpdater"/>
</lxslt:component>
<xsl:template match="CENTRAL">
<ROUTES>
<xsl:variable name="timestamp" select="js:timestamp(string(@C_DATA), string(@C_HORA))" />
<xsl:variable name="c_id" select="string(@R_ID_CENTRAL)" />
<!--<xsl:value-of select="js:indic(1,3)"/>-->
<xsl:for-each select="ROUTE">
<ROUTE>
<xsl:if test="METADADOS/@OP='I'">
<xsl:value-of select="metadados:insert($c_id, string(@R_ID), string(METADADOS/R_NUM), string(METADADOS/R_TERM_A), string(METADADOS/R_TERM_B), string(METADADOS/R_TYPE), string(METADADOS/R_DIR), string(METADADOS/R_EQUIP), string(METADADOS/R_CALC_TYPE), string(METADADOS/R_K1), string(METADADOS/R_K2), string(METADADOS/R_K3), string(METADADOS/R_K4), string(METADADOS/R_LABEL), string(METADADOS/R_ADDRESS), string(METADADOS/R_L), string(METADADOS/R_S), string(METADADOS/R_R), string(METADADOS/R_PPI_POI))"/>
</xsl:if>
<xsl:if test="METADADOS/@OP='U'">
<xsl:value-of select="metadados:update($c_id, string(@R_ID), string(METADADOS/R_NUM), string(METADADOS/R_TERM_A), string(METADADOS/R_TERM_B), string(METADADOS/R_TYPE), string(METADADOS/R_DIR), string(METADADOS/R_EQUIP), string(METADADOS/R_CALC_TYPE), string(METADADOS/R_K1), string(METADADOS/R_K2), string(METADADOS/R_K3), string(METADADOS/R_K4), string(METADADOS/R_LABEL), string(METADADOS/R_ADDRESS), string(METADADOS/R_L), string(METADADOS/R_S), string(METADADOS/R_R), string(METADADOS/R_PPI_POI))"/>
</xsl:if>
<xsl:if test="METADADOS/@OP='D'">
<xsl:value-of select="metadados:delete($c_id, @R_ID)"/>
</xsl:if>
<R_ID_CENTRAL><xsl:value-of select="$c_id"/></R_ID_CENTRAL>
<R_ID><xsl:value-of select="@R_ID"/></R_ID>
<R_TS><xsl:value-of select="$timestamp"/></R_TS>
<O_ATV><xsl:value-of select="DADOS/O_ATV"/></O_ATV>
<O_TOCR><xsl:value-of select="DADOS/O_TOCR"/></O_TOCR>
<O_CONG><xsl:value-of select="DADOS/O_CONG"/></O_CONG>
<O_BLOQ><xsl:value-of select="DADOS/O_BLOQ"/></O_BLOQ>
<O_OKR><xsl:value-of select="DADOS/O_OKR"/></O_OKR>
<O_OFR><xsl:value-of select="DADOS/O_OFR"/></O_OFR>
<I_ATV><xsl:value-of select="DADOS/I_ATV"/></I_ATV>
<I_TOCR><xsl:value-of select="DADOS/I_TOCR"/></I_TOCR>
<I_CONG><xsl:value-of select="DADOS/I_CONG"/></I_CONG>
<I_BLOQ><xsl:value-of select="DADOS/I_BLOQ"/></I_BLOQ>
<I_OKR><xsl:value-of select="DADOS/I_OKR"/></I_OKR>
<I_OFR><xsl:value-of select="DADOS/I_OFR"/></I_OFR>
<R_ESTADO>
<xsl:if test="METADADOS/R_CALC_TYPE='1'">
<xsl:value-of select="js:calc1(string(DADOS/O_ATV), string(DADOS/O_CONG), string(DADOS/O_OFR))" />
</xsl:if>
<xsl:if test="METADADOS/R_CALC_TYPE='2'">
<xsl:value-of select="js:calc2(string(DADOS/I_ATV), string(DADOS/O_CONG), string(DADOS/I_OFR))" />
</xsl:if>
<xsl:if test="METADADOS/R_CALC_TYPE='3'">
<xsl:value-of select="js:calc3(string(DADOS/I_ATV), string(DADOS/O_CONG), string(DADOS/I_OFR))" />
</xsl:if>
<xsl:if test="METADADOS/R_CALC_TYPE='4'">
<xsl:value-of select="js:calc4(string(DADOS/I_ATV), string(DADOS/O_CONG), string(DADOS/I_OFR))" />
</xsl:if>
</R_ESTADO>
</ROUTE>
</xsl:for-each>
</ROUTES>
</xsl:template>
</xsl:stylesheet>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing parameters in XSL to JavaScript