xml xslt example

Posted on 2014-07-16
Last Modified: 2014-07-27

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.
Question by:gudii9
    LVL 35

    Assisted Solution

    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

    LVL 7

    Author Comment

    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
    LVL 35

    Expert Comment

    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.
    LVL 21

    Expert Comment

    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:"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.
    LVL 7

    Author Comment

    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
    LVL 21

    Expert Comment

    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).
    LVL 7

    Author Comment

    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
    LVL 21

    Accepted Solution

    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

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    fix34  challenge 9 72
    allStar challenge 1 27
    word0 challenge 4 37
    wordappend challenge 8 51
    The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now