• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

xml xslt example


I am trying below example


books.xml looks like

<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>

XSLT file looks like

<!DOCTYPE html>

function loadXMLDoc(dname)
if (window.XMLHttpRequest)
  xhttp=new XMLHttpRequest();
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
try {xhttp.responseType="msxml-document"} catch(err) {} // Helping IE
return xhttp;

var x=loadXMLDoc("books.xml");
var xml=x.responseXML;

// code for IE
if (window.ActiveXObject || xhttp.responseType=="msxml-document")
for (i=0;i<nodes.length;i++)

// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();

while (result)


Open in new window

I am expected to see output like


what is the extension of the xpath i supposed to give.

How can i run this example myselef on my windows laptop machine with or without eclipse. Please advise.
  • 3
  • 3
  • 2
2 Solutions
The code you posted is not XSLT. But if you want to get the price you will need to change the xpath expression to


instead of

gudii9Author Commented:
The code you posted is not XSLT.

Is that is XPATH? what is the extension i supposed to give?
book.xml and then book.xpath would be file names of the xml and xpath files?

How do i run this example say using eclipse
Yes, the correct term is xpath. I don't know how to run it, it's using some active x objects, so I guess through IE.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

The example is an HTML document (as others have already commented).  It is using two files that MUST reside on the server.
books.html - The HTML document that drives the processing
bookx.xml - The XML data document

If you just try to open the files on the local file system, the javascript engine will crash with an access denied (it is a security violation to be able to access local files).

The line: xhttp.open("GET",dname,false); is the line that loads the XML document into memory.  The open() function is used to generate a HTTP request to retrieve the contents of the URL stored in 'dname'

To run the HTML locally, you will need to setup a HTTP server that will run under localhost.  The server can be any one of the following:
IIS - Web server that can be installed on Windows
Apache - Web server that can be installed on any OS (Windows, OSX, Linux...)

My recommendation is that you install Apache.  Since that works better with eclipse.
gudii9Author Commented:
I have apache tomcat integrated with eclipse. Just create regular java project and copy above xml and xslt to default package and run the xml like 'run as server' which talks to xslt and displays the html by applying styles?
please advise
As many people has already stated.  it is not xslt.  The file is a HTML file. To create the same in eclipse, create a new Project of type 'Static Web Project'.  The project should look like this:
Once the files are created, right click on the project and choose run as->Run on Server

On the browser window, navigate to the http://localhost:8080/Books/books.html (it will default to http://localhost:8080/Books/ with will generate an error).
gudii9Author Commented:
I got output as

So it is html file only not sxlt file.
what is dname and xmlhttprequest, ActivexObject and different code for different browser.
what is relationship between xslt and xpath. since in this example they did not use xslt at all but still they used xmpath(within html itself)
Please advise
There are multiple technologies in the example (and none of them is xslt).  XSLT is a language for transforming XML files the output of the XSLT transform can be another XML file or in some cases other formats (plain text, html, CSV...).  For a description of this language you can look at the wikipedia page.

For the technologies used in the example they are as follows:
XMLHttpRequest - Javascript API for creating dynamic requests (used in AJAX)
ActiveXObject - Technology used by Internet Explorer to run native code in browser
Microsoft.XMLHTTP - dynamic request component for creating dynamic requests in IE5.5 and IE6 (IE7-IE11 can use XMLHttpRequest)
XPATH - Query language for querying and navigating XML documents.  XPATH is a component of many languages and API's (XSLT, Java, C#,  Javascript)
Dynamic HTML - technique for creating HTML content at "runtime" (it is used in AJAX as well)

All of the links are from the wikipedia pages on each subject.  I am sure there are better tutorials for each of the technologies.  I supplied the links so you can get more general information if you desire.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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