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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with combine XSLT and JavaScript

Status
Not open for further replies.

li70

Technical User
Mar 24, 2007
10
GB
Hi. Everyone
I’m using an xslt stylesheet to transform my xml document to an html doc. In addition I need to include some pieces of JavaScript for validation purpose. JavaScript code needed to be placed between /* <![CDATA[*/ and /*]]>*/ in order to avoid parsing issue. So far so good, but what has derived me crazy is that why at run time the xslt processor weed out the <![DATA and ]]> from the target html document and just keep /**/. This is obvious by browsing the source code in the browser. And what surprise me is that the special JavaScript characters like ( < , > , &&) were converted into (&lt; , &amp;&amp;). As a result JavaScript code was frozen and the validation was escaped.
Here is my code
<html>
<head>
<script language="JavaScript">
/* <![CDATA[*/
function isvalid(){
var myelements=document.getElementsByTagName("input");
var counter=myelements.length
for (var i=0;i< counter;i++){
var para=myelements.value;
if (myelements.getAttribute("idnull")=='False'){
if (isEmpty(para)){
alert("please fill in the box !"+myelements.name)
return false
}
}
} //for
return true;
} //isvalid

function isEmpty(para){
var fieldname=para;
if (fieldname== null || fieldname == ""){
return true;
}
return false;
} // is Empty
/* ]]> */
</script>
</head>
<body>
<h4>
<i> All fields are required. </i>
</h4>
<form name="form1" method="GET" onSubmit="return isvalid()" >
id_card<input type="text" name="id_card" idnull="False"/>
<br/>
address<input type="text" name="address" idnull="False"/>
<br/>
d_of_birth<input type="text" name="d_of_birth" idnull="False"/>
<br/>
<input type="reset" value="Clear All Fields"/>
<input type="submit" VALUE="SUBMIT"/>
</form>
</body>
</html>



any clue?

Many thanks in advance
 
Can you post your stylesheet?

---------------------------------------
TINSTAAFL, which is why I contribute.
 
>JavaScript code needed to be placed between /* <![CDATA[*/ and /*]]>*/ in order to avoid parsing issue.
I don't think so. Why do you need that artifact?

If the script section contents are wrapped inside as CDATA section and that it is appearing in the xslt document's template, I don't see the problem you described. In principle, it should work this way.
[tt]
<xsl:template match="/"> [green]<!-- or match something else -->[/green]
<html>
<head>
<script language="JavaScript">
[red]<![CDATA[[/red]
function isvalid(){
var myelements=document.getElementsByTagName("input");
[blue]//etc etc...[/blue]
}
[blue]//etc etc...[/blue]
[red]]]>[/red]
</script>
<!-- etc etc...!>[/blue]
</xsl:template>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top