Jump to content

Recommended Posts

From w3 schools: http://www.w3schools.com/xpath/xpath_examples.asp which uses a book xml example which looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>

<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>
</bookstore> 

 

I'm trying to learn how to use xml but i can't seem to get it with w3schools. I want to be able to just select the book of 'category="COOKING"'

 

In the example - http://www.w3schools.com/xpath/tryit.asp?filename=try_xpath_select_pricenodes_35  it manages to select all prices under 35 by using the following path:

path="/bookstore/book[price>35]/price";

 

How can I change this to something like:

path="/bookstore/book[category="COOKING"]/price";

I don' think I asked my question correctly.

 

The following example from w3 schools uses this xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>

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

 

And then the following script gets and prints on a new line all the text from the price tags.

<html>
<body>
<script type="text/javascript">
	function loadXMLDoc(dname){
		if (window.XMLHttpRequest){
			xhttp=new XMLHttpRequest();
  			}
		else{
			xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		xhttp.open("GET",dname,false);
		xhttp.send("");
		return xhttp.responseXML;
	}
	//getting the xml into a var to parse		
	xml=loadXMLDoc("books.xml");


	//the path in question
	path="/bookstore/book/price/text()"   ////how can i change this to only select the prices of the books where the category="COOKING" ?


	// code for IE
	if (window.ActiveXObject){
		var nodes=xml.selectNodes(path);

		for (i=0;i<nodes.length;i++)
			{
				document.write(nodes[i].nodeValue);
				document.write("<br />");
			}
	}
	// code for Mozilla, 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.nodeValue + "<br />");
				result=nodes.iterateNext();
			}
	}
</script>
</body>
</html>

 

 

Is it possible to change the path so that the script will only print out the text of the prices tag where parent tag 'book' has a category of 'COOKING'  ??

 

I'm really stuck and can't seem to find out how to get it. Any help would awesome :)

path="/bookstore/book/price/text()"

 

This line is XPath.. Think of it like a SQL query to select certain data matching a pattern. You just need to run a check on the attribute:

 

path="/bookstore/book[@category='COOKING']/price/text()"

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.