Jump to content

About XML/XSLT and Javascript:Plz Urgent Help Me


Guest pravis25
 Share

Recommended Posts

Guest pravis25

pravis25@gmail.comHai , I am new to XSL.I am developing one module which involves XML and XSL .I have developed code for transforming xml to xhtml using xsl.(Only Displaying is done).In my form two radio buttons one dropdown box and two text boxes are there .). All the information which ever is displayed coming from XML ., ----- But I want to validate this elements (which are in xslt ,ie checking text box is empty or not ..) Where I have to add script in XSL ., Plz give simple example on this-----------I am sending code of xml file and xsl plz have a look at this.,All data is coming from xml file into xsl to display .. Hotel.xml

<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="Hotels.xsl"?><Services>	<Service>  <Hotels type="radio" title="Hotel" groupname="service" default="true"/>  <HotelsFlights type="radio" title="Flight+Hotel" groupname="service"/>	</Service>	<Hotels>  <Destination title_d="Destination :">   	 <DestinationPlace type="OPTION" id=""  title_d="Select Destination" value_d="Select Destination" defalut="true"/> 	 <DestinationPlace type="OPTION" id=""  title_d="Aruba" value_d="Aruba" />  </Destination>	  <HotelChoice title_hc="Hotels :"> 	 <choice type="OPTION" id=" " title_h="AmsterDam Manor Beach" value_h="AmsterDam Manor Beach"/> 	 <choice type="OPTION" id=" " title_h="Wyndham Aruba Beach" value_h="Wyndham Aruba Beach"/>  </HotelChoice>  <Dates> 	 <CheckIn type="INPUT" title_dt="Check In :" id="chkin" name="chkin" size_dt="10" maxlength="10">mm/dd/yyyy</CheckIn> 	 <CheckOut type="INPUT" title_dt="Check Out :" name="chkout" size_dt="10" maxlength="10">mm/dd/yyyy</CheckOut>  </Dates>    <RoomNos title="No.of Rooms :"> 	 <room type="OPTION" id="" titl="1" valu="1"/> 	 <room type="OPTION" id="" titl="1" valu="2"/> 	 <room type="OPTION" id="" titl="1" valu="3"/>  </RoomNos>  <Submit tile="Search :" type="submit" id="search" value="Search"/>	</Hotels></Services>

============================================= Hotels.xsl-

<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/">	<html>	<head>	<script language="javascript">  	function RadioFunc()	{  var result;  alert(" Radio ");  result +=document.write("<TABLE >");  result +=document.write("<TR><TD>"+'Praveen'+"</TD></TR>");  result +=document.write("</TABLE>");  alert("Result "+result);	}	function validateForm()	{  alert("Before validating");  var chkindate= document.getElementById(chkin).value;   alert(chkindate);  }	</script>	</head> 	</html>	<xsl:apply-templates/><xsl:if test="@value_d='Select Destination'">	alert("Please Enter Destination");</xsl:if></xsl:template><xsl:template match="Services" >	<form name="myform" action="two.jsp" method="get" >  <table bgcolor="lightgreen" width="350" height="200" border="1" >  <tr><td><xsl:apply-templates/></td></tr>  </table>		</form>  </xsl:template><xsl:template match="Service">	<table border="0" align="center" valign="top"><tr>	<xsl:for-each select="*">  <td align="left"><xsl:value-of select="@title"/></td>  <td align="center">    <input type="{@type}" onClick="RadioFunc()"> 	 <xsl:attribute name="Name">    <xsl:value-of select="@groupname"/> 	 </xsl:attribute> 	 <xsl:attribute name="value">    <xsl:value-of select="@value"/> 	 </xsl:attribute> 	 <xsl:if test="@default='true'">    <xsl:attribute name="checked"/> 	 </xsl:if>  </input>    </td>	</xsl:for-each>	</tr></table><br/></xsl:template><xsl:template match="Hotels/Destination"><xsl:value-of select="@title_d"/>	<SELECT id="dest" name="dest">      <xsl:for-each select="*">     <xsl:element name="{@type}"> 	 <xsl:value-of select="@value_d"/>  </xsl:element>       </xsl:for-each>  	</SELECT> </xsl:template><xsl:template match="Hotels/HotelChoice"><xsl:value-of select="@title_hc"/>      <SELECT id="hcid" name="hcid">	<xsl:for-each select="*">  <xsl:element name="{@type}"> 	 <xsl:value-of select="@value_h"/>  </xsl:element>	</xsl:for-each>     </SELECT><br/> </xsl:template><xsl:template match="Hotels/Dates"><table><tr><xsl:for-each select="*">  <td> 	 <xsl:value-of select="@title_dt"/>  </td>  <td>  <xsl:element name="{@type}" >    <xsl:attribute name="size">    <xsl:value-of select="@size_dt"/> 	 </xsl:attribute>   	 <xsl:attribute name="maxlength">    <xsl:value-of select="@maxlength"/> 	 </xsl:attribute> 	 <xsl:attribute name="value">    <xsl:value-of select="text()"/> 	 </xsl:attribute>  </xsl:element>  </td> </xsl:for-each></tr></table></xsl:template><xsl:template match="Hotels/RoomNos"><table align="center"><th>Travellers</th><tr><td><xsl:value-of select="@title"/></td>    <td><SELECT id="rooms" name="rooms">  <xsl:for-each select="*"> 	 <xsl:element name="{@type}">     <xsl:value-of select="@valu"/> 	 </xsl:element>  </xsl:for-each>          </SELECT><br/></td></tr>	</table><br/> </xsl:template><xsl:template match="Hotels/Submit">	<table align="center" border="0" >	<tr>	<td align="center">  <input type="{@type}" onclick="validateForm();"> 	 <xsl:attribute name="value">    <xsl:value-of select="@value"/> 	 </xsl:attribute>  </input>	</td>	</tr>	</table></xsl:template></xsl:styleshhet>

Link to comment
Share on other sites

I know it's not exactly the help you're looking for, but here's the output of your code as seen with a server side script:

<html><head><script language="javascript"> function RadioFunc(){ var result; alert(" Radio "); result +=document.write("<TABLE>"); result +=document.write("<TR><TD>"+'Praveen'+"</TD></TR>"); result +=document.write("</TABLE>"); alert("Result "+result);}function validateForm(){ alert("Before validating"); var chkindate= document.getElementById(chkin).value;  alert(chkindate); }</script></head></html><form method="get" action="two.jsp" name="myform"><table border="1" height="200" width="350" bgcolor="lightgreen"><tr><td><table valign="top" align="center" border="0"><tr><td align="left">Hotel</td><td align="center"><input onClick="RadioFunc()" type="radio" Name="service" value="" checked></td><td align="left">Flight+Hotel</td><td align="center"><input onClick="RadioFunc()" type="radio" Name="service" value=""></td></tr></table><br> Destination :<SELECT name="dest" id="dest"><OPTION>Select Destination</OPTION><OPTION>Aruba</OPTION></SELECT> Hotels :<SELECT name="hcid" id="hcid"><OPTION>AmsterDam Manor Beach</OPTION><OPTION>Wyndham Aruba Beach</OPTION></SELECT><br> <table><tr><td>Check In :</td><td><INPUT size="10" maxlength="10" value="mm/dd/yyyy"></td><td>Check Out :</td><td><INPUT size="10" maxlength="10" value="mm/dd/yyyy"></td></tr></table>  <table align="center"><th>Travellers</th><tr><td>No.of Rooms :</td><td><SELECT name="rooms" id="rooms"><OPTION>1</OPTION><OPTION>2</OPTION><OPTION>3</OPTION></SELECT><br></td></tr></table><br> <table border="0" align="center"><tr><td align="center"><input onclick="validateForm();" type="submit" value="Search"></td></tr></table></td></tr></table></form>

Just to fix this... replace

</html><xsl:apply-templates/><xsl:if test="@value_d='Select Destination'">alert("Please Enter Destination");</xsl:if>

With

<body><xsl:apply-templates/><xsl:if test="@value_d='Select Destination'">alert("Please Enter Destination");</xsl:if></body></html>

As for the issue itself, I admit I'm lost a bit. It doesn't work when the page is parsed sirver side, but it works on the browser side, so it's kind a shooting in the dark. I'll see what I can do though.Btw, the end tag of <xsl:stylesheet> is </xsl:stylesheet>, not "</xsl:styleshhet>" (minor spelling error).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...