2
votes

I need your help, at first I start on code:

bookmod.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>

<book category="COOKING">
    <insystem>
        <t>asdgas</t>
        <b>asdgas</b>
    </insystem>
    <data>
  <Data name="title">Everyday Italian</Data>
  <Data name="author">Giada De Laurentiis</Data>
  <Data name="year">2005</Data>
  <Data name="price">30.00</Data>
  </data>
</book>

<book category="CHILDREN">
    <insystem>
        <t>asdgas</t>
        <b>asdgas</b>
    </insystem>
    <data>

  <Data name="title">Potter Potter</Data>
  <Data name="author">Giada De Laurentiis</Data>
  <Data name="year">2003</Data>
  <Data name="price">30.00</Data>
    </data>
</book>

<book category="WEB">
    <insystem>
        <t>asdgas</t>
        <b>asdgas</b>
    </insystem>
    <data>

  <Data name="title">asdgasd Potter</Data>
  <Data name="author">Giada De Laurentiis</Data>
  <Data name="year">2005</Data>
  <Data name="price">30.00</Data>
    </data>
</book>

<book category="WEB">
    <insystem>
        <t>asdgas</t>
        <b>asdgas</b>
    </insystem>
    <data>

  <Data name="title">web book</Data>
  <Data name="author">Giada De Laurentiis</Data>
  <Data name="year">2005</Data>
  <Data name="price">30.00</Data>
    </data>
</book>

</bookstore>

index.php

<!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("bookmod.xml");
var xml=x.responseXML;
path="/bookstore/book/data/Data";

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

In Path I try also : /bookstore/book/data[Data=2005]/Data[@name='year']

and many other...

It basically means that an XML file is quite large and I need to find only those where <data name="year"> == 2005

I tried many ways and generators, but still can not deal with it :(

It would have been nice if you could enter a title and author.


Edit:

<book xmlns="http://schemas.microsoft.com/win/2004/08/events/event">

I do not know why but for it not working

Do you know a method to get around this? when I delete xmlns="..... everything works well but I have it.

2

2 Answers

1
votes

The following xpath should provide what you need basically with following inputs : year, title, author. It is a bit like up and down traversal.

/bookstore/book/data/Data[@name='year'][.='2005']/preceding-sibling::*[@name='title'][.='Everyday Italian']/following-sibling::*[@name='author'][.='Giada De Laurentiis']/parent::*

You can replace the 2005, Everyday Italian, Giada De Laurentiis with the inputs you like.

The way it works is , it first gets Data with year = 2005 then gets its preceding-sibling which are title, author then gets matching title and then gets its following sibling whose author matches given input, finally its parent to return whole data element.

Output for the above xpath is :

'<data> <Data name="title">Everyday Italian</Data> <Data name="author">Giada De Laurentiis</Data> <Data name="year">2005</Data> <Data name="price">30.00</Data> </data>'

Output for a different input : 2003, Potter Potter, Giada De Laurentiis

'<data> <Data name="title">Potter Potter</Data> <Data name="author">Giada De Laurentiis</Data> <Data name="year">2003</Data> <Data name="price">30.00</Data> </data>'

2
votes

The following XPath will select all book elements from 2005:

/bookstore/book[data/Data[@name='year' and .=2005]]