Xml transformations to xhtml not working

Dear Experts,

I am able to display the grid of products from xml in XHTML. But i am struck in displaying the details of particular product when it is clicked. Since i am new to xml concept, can someone help in the syntax please ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MurfurFull Stack DeveloperCommented:
Couple of things:

1. you need to have an unique ID of some sort in your XML data to identify each pet
2. you need to modify your XSL to use the unique ID so that each record in the initial display table is identifiable
3. you need to have a click function on the page to dictate which pet's details you wish to display in the main box

So, I modified your XML adding the ID tag and a unique value per record and I also changed the data as both records of data were identical which makes testing a little tricky!
I modified the XSL to put the ID value on each record and I added a class to make the table row identifiable for the click function.
I modified the script adding jQuery, and moving the XML load variables outside the functions to make them global
Now when you click on a row of the initial table it captures the ID value, finds the XML record that matches that ID and returns the corresponding data fields
<?xml version="1.0" encoding="utf-8"?>
			<Botanical>Smallus Dogus Jacksonium</Botanical>
		<Breed>West Highland Terrier</Breed>
		<Age>Adult 3yr</Age>
			<Botanical>Sub Groundium Treasury</Botanical>
		<Age>Adult 2yr</Age>


Open in new window

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
<xsl:template match="/">
	<table border="1">
		<tr bgcolor="#9acd32">

	<xsl:for-each select="pets/pet">

		<tr id="{id}" class="pet">
			<td><xsl:value-of select="Name/Common" /></td>
			<td><img src="{Image}" /></td>


Open in new window

	<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

		$(document).ready(function() {

			//	load XML data and style
			var xml = loadXMLDoc("newxml.xml");
			var xsl = loadXMLDoc("newxml.xsl");
			//	display initial table of data
			function loadXMLDoc(filename)
				if (window.ActiveXObject)
					xhttp = new ActiveXObject("Msxml2.XMLHTTP");
				else {
					xhttp = new XMLHttpRequest();
				xhttp.open("GET", filename, false);
				try {
					xhttp.responseType = "msxml-document"
				} catch(err) {} // Helping IE11
				return xhttp.responseXML;
			function displayResult()
				// code for IE
				if (window.ActiveXObject || xhttp.responseType == "msxml-document")
					ex = xml.transformNode(xsl);
					document.getElementById("example").innerHTML = ex;
				// code for Chrome, Firefox, Opera, etc.
				else if (document.implementation && document.implementation.createDocument)
					xsltProcessor = new XSLTProcessor();
					resultDocument = xsltProcessor.transformToFragment(xml, document);
			// react to click of table row with a class of pet
				//	get the ID value of the clicked row
				var thisID = $(this).attr('id');
				//	find the corresponding data block in the XML source
				myXML = $(xml).find("pet").filter(function() {
					//	gets only elements relating to button ID clicked
					return $(this).find('id').text() == thisID;
				// build the output data plating tags and values in separate columns
				var display = myXML.children().map(function() {
					return '<tr><td>' + this.tagName + '</td><td>' + $(this).text() + '</td></tr>';
				}).get().join(' ');
				//	top & tail the table tags
				var output = "<table>" + display + "</table>";
				//	send the output data to the display DIV
				$( "#details" ).html( output );
				//	apply a style to the display DIV
				$( "#details" ).css('border','1px solid #333');



	<div id="example"> </div>
	<div id="blank">&nbsp;</div>
	<div id="details"> </div>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.