 |
| 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
|
|
|
|

January 23rd, 2012, 12:48 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
java included in xslt
Hi everyone,
I have this :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="TEXT/xml" href="testForm.xsl"?>
<form>
<SELECT name="element">
<OPTION value="button">Button</OPTION>
<OPTION value="text"> Textbox</OPTION>
<OPTION value="radio">Radio</OPTION>
<OPTION value ="label">Label</OPTION>
<OPTION value ="checkbox">Checkbox</OPTION>
<OPTION value ="dropdownlist">DropDownList</OPTION>
</SELECT>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<span id ="tableIDBar"></span>
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" /></TD>
</TR>
</TABLE>
</form>
and xslt:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="no" media-type="TEXT/html" indent="yes"/>
<xsl:template match="/form">
<html>
<head>
<TITLE> Add/Remove dynamic controls in HTML table </TITLE>
<xsl:text disable-output-escaping="yes">
<xsl:script implements-prefix="local" language="JScript"><![CDATA[
function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
//cell3.document.getElementById("tableIDBar");
var element2 = document.createElement("input");
//element2 = document.setAtribute("text");
//element2.setAttribute("tableID",tableID);
//element2.setAttribute("value",tableID);
//element2.setAttribute("name",tableID);
element2.type= "text";
cell3.appendChild(element2);
//element2.type = "button";
//cell3.appendChild(element2);
}//end function
]]></xsl:script>
</xsl:text>
</head>
<body>
</body>
</html>
Name:
<br/>
</xsl:template>
</xsl:stylesheet>
The java function is to add textfields but the javascsript doesnt work.
THX for helping.
|
|

January 23rd, 2012, 12:53 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Simply use a literal result element for the script e.g.
Code:
<script type="text/javascript">
function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
//cell3.document.getElementById("tableIDBar");
var element2 = document.createElement("input");
//element2 = document.setAtribute("text");
//element2.setAttribute("tableID",tableID);
//element2.setAttribute("value",tableID);
//element2.setAttribute("name",tableID);
element2.type= "text";
cell3.appendChild(element2);
//element2.type = "button";
//cell3.appendChild(element2);
}
</script>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

January 23rd, 2012, 01:05 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
xsl:script was introduced in the XSLT 1.1 Working Draft which was withdrawn a few months after publication (around 2001?). It was designed to allow Javascript code to be executed during the transformation.
You seem to be doing something different, namely adding Javascript code to the generated HTML page. For that, just create a script element in the same way as any other HTML element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 23rd, 2012, 03:41 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have it now like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="TEXT/xml" href="testFoom.xsl"?>
<form>
<SELECT name="element">
<OPTION value="button">Button</OPTION>
<OPTION value="text"> Textbox</OPTION>
<OPTION value="radio">Radio</OPTION>
<OPTION value ="label">Label</OPTION>
<OPTION value ="checkbox">Checkbox</OPTION>
<OPTION value ="dropdownlist">DropDownList</OPTION>
</SELECT>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<span id ="tableIDBar"></span>
<TABLE id="dataTable" width="350px" border="1">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" /></TD>
</TR>
</TABLE>
</form>
and xslt:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="no" media-type="TEXT/html" indent="yes"/>
<xsl:template match="/form">
<html>
<head>
<TITLE> Add/Remove dynamic controls in HTML table </TITLE>
<script type="text/javascript">
function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
//cell3.document.getElementById("tableIDBar");
var element2 = document.createElement("input");
//element2 = document.setAtribute("text");
//element2.setAttribute("tableID",tableID);
//element2.setAttribute("value",tableID);
//element2.setAttribute("name",tableID);
element2.type= "text";
cell3.appendChild(element2);
//element2.type = "button";
//cell3.appendChild(element2);
}
</script>
</head>
<body>
</body>
</html>
Name:
<br/>
</xsl:template>
</xsl:stylesheet>
But I get this as output:
Button Textbox Radio Label Checkbox DropDownList 1
:(
|
|

January 24th, 2012, 05:56 AM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The javascript still doesnt work like this way
|
|

January 24th, 2012, 06:09 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
When you say you get this output, do you mean that is what the browser shows? Please show us the actual HTML generated, and contrast it with the HTML you are trying to generate. The HTML you are generating doesn't make much sense, for example it has text after the `</html>` end tag.
When you say "The Javascript doesn't work like this way" please explain how you expect it to work and how the actual behaviour differs.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 24th, 2012, 06:31 AM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
it has to create dynamic form controls, like textfields. So u can add/delete textfields. It is working if u only run the javascript. But not if u run it with xslt.
|
|

January 24th, 2012, 06:48 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I asked some specific questions because the answers would help me to advise you. Since you're ignoring my questions, I'm afraid I'm going to have to give up.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

January 25th, 2012, 03:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
Check if the following helps:
Code:
<xsl:template match="form">
<html>
<head>
<TITLE> Add/Remove dynamic controls in HTML table </TITLE>
</head>
<body>
<script type="text/javascript">
function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
//cell3.document.getElementById("tableIDBar");
var element2 = document.createElement("input");
//element2 = document.setAtribute("text");
//element2.setAttribute("tableID",tableID);
//element2.setAttribute("value",tableID);
//element2.setAttribute("name",tableID);
element2.type= "text";
cell3.appendChild(element2);
//element2.type = "button";
//cell3.appendChild(element2);
}
</script>
<xsl:copy-of select="."/>
Name:
<br/>
</body>
</html>
</xsl:template>
__________________
Rummy
|
|

January 25th, 2012, 08:51 AM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi everyone,
I have it now like this:
xsl:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="no" media-type="TEXT/html" indent="yes"/>
<xsl:template match="/FORM">
<html>
<head>
<TITLE>Add/Remove dynamic controls in HTML table </TITLE>
<script type="text/javascript">
recoverInputs( document.forms.myform, readFromFile(), true );
function addRow(tableID)
{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
//cell3.document.getElementById("tableIDBar");
var element2 = document.createElement("input");
//element2 = document.setAtribute("text");
//element2.setAttribute("tableID",tableID);
//element2.setAttribute("value",tableID);
//element2.setAttribute("name",tableID);
element2.type= "text";
cell3.appendChild(element2);
//element2.type = "button";
//cell3.appendChild(element2);
}//en function
//Begin function to initiate:
function init()
{
var srcTree = new ActiveXObject("Msxml2.DOMDocument.6.0");
srcTree.async=false;
// You can substitute other XML file names here.
srcTree.load("C:\SaveData\form5.xml");
var xsltTree= new ActiveXObject("Msxml2.DOMDocument.6.0");
xsltTree.async = false;
// You can substitute other XSLT file names here.
xsltTree.load("C:\SaveData\form5.xsl");
resTree.innerHTML = srcTree.transformNode(xsltTree);
}
//function
//End function
//Begin function
var willWork = false;
if( window.ActiveXObject ) {
//get a reference to the file system
window.onerror = function () {
alert( 'Your security settings are too high for this script '+
'or scripting host is not correctly installed on your computer' );
return true;
};
var FSO = new ActiveXObject("Scripting.FileSystemObject");
//specify the file name
var tempFile = 'myfile.txt';
//specify the folder name (try 'My Documents' first):
try {
//use scripting host to get the path to a special folder
//this does not work on all installations - including one of mine (no idea why)
var tempFolder = ( new ActiveXObject("WScript.Shell") ).SpecialFolders("MyDocuments");
} catch(e) {
//error for no good reason - revert to using temp directory
//WARNING: ALL users of this computer will be able to see the file
var tempFolder = 'c:\\temp';
}
//if the folder exists, we are on a standard Windows installation
if( FSO.FolderExists( tempFolder ) ) {
willWork = true;
}
}
if( !willWork ) {
alert( 'This only works with a standard installation of'+
' Internet Explorer on Microsoft Windows' );
}
function saveToFile( oText ) {
if( !willWork ) { return; }
var theFile = FSO.OpenTextFile( tempFolder + '\\' + tempFile, 2, true );
theFile.write( oText );
theFile.close();
}
function readFromFile() {
if( !willWork ) { return ''; }
if( FSO.FileExists( tempFolder + '\\' + tempFile ) ) {
var theFile = FSO.OpenTextFile( tempFolder + '\\' + tempFile, 1, false );
var oOut = theFile.readAll();
theFile.close();
return oOut;
} else {
return null;
}
}
//other function
//End otherfunciton
</script>
</head>
<body>
</body>
</html>
Name:
<SELECT name="element">
<OPTION value="button">Button</OPTION>
<OPTION value="text"> Textbox</OPTION>
<OPTION value="radio">Radio</OPTION>
<OPTION value ="label">Label</OPTION>
<OPTION value ="checkbox">Checkbox</OPTION>
<OPTION value ="dropdownlist">DropDownList</OPTION>
</SELECT>
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<INPUT type="button" value="Safe form" onclick="CreateXMLStringParser(XMLstring)" />
<input type="button" value="Save Form Test" onclick="saveToFile('inpVal',getFormString(this.form,true));"/>
<INPUT type="button" value="Recover" onclick="recoverInputs(this.form,retrieveCookie('inpVal'),true);"/>
<span id ="tableIDBar"></span>
<TABLE id="dataTable" width="350px" border="0">
<TR>
<TD><INPUT type="checkbox" name="chk"/></TD>
<TD> 1 </TD>
<TD> <INPUT type="text" /></TD>
</TR>
</TABLE>
<xsl:attribute name="type"><xsl:value-of select="item_type"/></xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="item_name"/></xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="item_value"/></xsl:attribute>
<br/>
</xsl:template>
</xsl:stylesheet>
And xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="TEXT/xml" href="form5.xsl"?>
<FORM>
</FORM>
And it works. But now I want to safe the form controls(textfields). So not the user data but the actual form controls. So that it will generate an html page saved in a folder.
But how to do that?
THX
|
|
 |