2

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.

bradley546994
  • 630
  • 2
  • 12
  • 30

2 Answers2

2

The following XPath will select all book elements from 2005:

/bookstore/book[data/Data[@name='year' and .=2005]]
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • do you know any debugger ? – bradley546994 Jul 14 '16 at 09:09
  • Your edit adds a namespace, which effectively changes the element name of both `book` and its descendants. You can thwart the namespace by testing `local-name()`, or preferably defining a namespace prefix and using it: See [**XPath without using local-name() or name() functions**](http://stackoverflow.com/questions/26333888/xpath-without-using-local-name-or-name-functions), or ask a new question please. Thanks. – kjhughes Jul 14 '16 at 12:24
1

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

SomeDude
  • 13,876
  • 5
  • 21
  • 44
  • In this file all working well but I have a similar file built very similar but does not work for me this method. /Events/Event/data/Data[@Name='day'][.='101']/preceding-sibling::*[@name='Adapter']"; – bradley546994 Jul 14 '16 at 07:11
  • For the first question please add your new xml so I can take a look to see what might be wrong. for the second one, please use `local-name()` like `//*[local-name()='book']` – SomeDude Jul 14 '16 at 15:16