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 ?

Thanks
newhtml.html
newxml.xml
newxml.xsl
detailpage.jpg
codeoxygenAsked:
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"?>
<pets>
	<pet>
		<id>1</id>
		<Name>
			<Common>Jacko</Common>
			<Botanical>Smallus Dogus Jacksonium</Botanical>
		</Name>
		<Type>Dog</Type>
		<Breed>West Highland Terrier</Breed>
		<Sex>male</Sex>
		<Size>Small</Size>
		<Colour>White</Colour>
		<Age>Adult 3yr</Age>
		<Image>pet-images/boxer-dog-lying-down-thumbnail.jpg</Image>
	</pet>
	
	<pet>
		<id>2</id>
		<Name>
			<Common>Petra</Common>
			<Botanical>Sub Groundium Treasury</Botanical>
		</Name>
		<Type>Cat</Type>
		<Breed>Siamese</Breed>
		<Sex>Female</Sex>
		<Size>Standard</Size>
		<Colour>Tan</Colour>
		<Age>Adult 2yr</Age>
		<Image>pet-images/boxer-dog-lying-down-thumbnail.jpg</Image>
	</pet>

</pets>

Open in new window

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <!--html>
  <body-->
	<h2>Categories</h2>
	
	<table border="1">
		<tr bgcolor="#9acd32">
			<th>Name</th>
			<th>Image</th>
		</tr>

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

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

	</xsl:for-each>
	
	</table>
  <!--/body>
  </html-->
 
</xsl:template>
</xsl:stylesheet>

Open in new window

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

		$(document).ready(function() {

			//	load XML data and style
			var xml = loadXMLDoc("newxml.xml");
			var xsl = loadXMLDoc("newxml.xsl");
			
			//	display initial table of data
			displayResult();
	
			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
			
				xhttp.send("");
				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();
					xsltProcessor.importStylesheet(xsl);
					resultDocument = xsltProcessor.transformToFragment(xml, document);
					document.getElementById("example").appendChild(resultDocument);
				}
			}
		
			// react to click of table row with a class of pet
			$('tr.pet').click(function(){
			
				//	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');

			});


		});
	
	</script>

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

Open in new window

0

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
XML

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.