javascript - Set Click event on value display in Html file and want to show there child Element When Clicked on it -
javascript - Set Click event on value display in Html file and want to show there child Element When Clicked on it -
this xml file:-
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="final.xsl"?> <root> <child1 entity_id = "1" value= "asia"> <child2 entity_id = "2" value = "india"> <child3 entity_id = "3" value = "gujarat"> <child5 entity_id = "5" value ="rajkot"></child5> </child3> <child4 entity_id = "4" value = "rajshthan"> <child6 entity_id = "6" value = "ajmer"></child6> </child4> </child2> </child1> </root> this xslt file:-
<xsl:template match="*"> <xsl:for-each select="//child2"> <xsl:value-of select="*"/> <xsl:value-of select="@value" /> </xsl:for-each> this html code:-
<script> function loadxmldoc(dname) { if (window.xmlhttprequest) { xhttp=new xmlhttprequest(); } else { xhttp=new activexobject("microsoft.xmlhttp"); } xhttp.open("get",dname,false); xhttp.send(""); homecoming xhttp.responsexml; } function displayresult() { xml=loadxmldoc("final.xml"); xsl=loadxmldoc("books05.xsl"); // code ie if (window.activexobject) { xml.addparameter("rss", test); ex=xml.transformnode(xsl); document.getelementbyid("load").innerhtml=ex; } // code mozilla, firefox, opera, etc. else if (document.implementation && document.implementation.createdocument) { xsltprocessor=new xsltprocessor(); xsltprocessor.importstylesheet(xsl); resultdocument = xsltprocessor.transformtofragment(xml,document); document.getelementbyid("load").appendchild(resultdocument); } } </script> <body onload="displayresult()"> <div id="load" /> </body> output is:-
india now want set click event on india... clicked on republic of india display there kid element gujarat , rajshthan... hide republic of india when show gujarat , rajshthan...
this should work. first can define xslt this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/> <xsl:param name="selected" /> <xsl:template match="/*" priority="2"> <xsl:apply-templates select="*[not($selected)] | //*[@entity_id = $selected]/*" /> </xsl:template> <xsl:template match="*[*]"> <span onclick="displayresult('{@entity_id}');"> <xsl:call-template name="itemtext" /> </span> </xsl:template> <xsl:template match="*" name="itemtext"> <xsl:value-of select="concat(@value, ' ')"/> </xsl:template> </xsl:stylesheet> when run without parameters, produces list of top level items, onclick handlers phone call javascript function own entity_id:
<span onclick="displayresult('1');">asia </span> and when run entity_id value selected parameter, returns list of children of item entity id:
<span onclick="displayresult('3');">gujarat </span> <span onclick="displayresult('4');">rajshthan </span> then, can write html page this:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>untitled page</title> <script> var xml = loadxmldoc("final.xml"); var xsl = loadxmldoc("books05.xsl"); function loadxmldocactivex(location) { var doc = new activexobject("msxml2.freethreadeddomdocument"); doc.async = false; doc.load(location); homecoming doc; } function loadxmldocother(location) { xhttp = new xmlhttprequest(); xhttp.open("get", location, false); xhttp.send(""); homecoming xhttp.responsexml; } function loadxmldoc(dname) { if (window.activexobject) { homecoming loadxmldocactivex(dname); } else if (window.xmlhttprequest) { homecoming loadxmldocother(dname); } } function transformactivex(xml, xsl, target, selected) { var transform = new activexobject("msxml2.xsltemplate"); transform.stylesheet = xsl; var processor = transform.createprocessor(); processor.input = xml; if (selected) { processor.addparameter("selected", selected); } processor.transform(); target.innerhtml = processor.output; } function transformother(xml, xsl, target, selected) { var xsltprocessor = new xsltprocessor(); xsltprocessor.importstylesheet(xsl); if (selected) { xsltprocessor.setparameter(null, "selected", selected); } var resultdocument = xsltprocessor.transformtofragment(xml, document); target.innerhtml = ""; target.appendchild(resultdocument); } function displayresult(selected) { var targetelement = document.getelementbyid("load"); // code ie if (window.activexobject) { transformactivex(xml, xsl, targetelement, selected); } // code mozilla, firefox, opera, etc. else if (document.implementation && document.implementation.createdocument) { transformother(xml, xsl, targetelement, selected); } } </script> </head> <body onload="displayresult()"> <div id="load"> </div> </body> </html> notice how displayresult() function has parameter, , when parameter has value, passes value xslt xslt parameter.
that should it!
javascript jquery xslt xpath
Comments
Post a Comment