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

xml xslt example

Hi,

I am trying below example

http://www.w3schools.com/XPath/tryit.asp?filename=try_xpath_select_pricenodes_text

books.xml looks like

<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<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>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>



XSLT file looks like


<!DOCTYPE html>
<html>
<body>
<script>

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

var x=loadXMLDoc("books.xml");
var xml=x.responseXML;
path="/bookstore/book/title";

// code for IE
if (window.ActiveXObject || xhttp.responseType=="msxml-document")
{
xml.setProperty("SelectionLanguage","XPath");
nodes=xml.selectNodes(path);
for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br>");
  }
}

// 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)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br>");
  result=nodes.iterateNext();
  }
}

</script>
</body>
</html>

Open in new window


I am expected to see output like


30.00
29.99
49.99
39.95

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.
0
gudii9
Asked:
gudii9
  • 3
  • 3
  • 2
2 Solutions
 
girionisCommented:
The code you posted is not XSLT. But if you want to get the price you will need to change the xpath expression to

/bookstore/book/price

instead of

/bookstore/book/title
0
 
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
0
 
girionisCommented:
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.
0
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.

 
MogalManicCommented:
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.
0
 
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
0
 
MogalManicCommented:
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:
BooksProject.PNG
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).
0
 
gudii9Author Commented:
I got output as
http://localhost:8080/Books/books.html

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
0
 
MogalManicCommented:
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.
0

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