Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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
0
codeoxygen
Asked:
codeoxygen
1 Solution
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now