I have read lots of Xpath tutorials but don't know how to query a xml document. So I have an XML document and a query like this /bookstore/book/title How do I conduct the search? Sorry if this question is badly phrased but I am very confused.

Recommended Answers

All 8 Replies

I have read lots of Xpath tutorials but don't know how to query a xml document. So I have an XML document and a query like this /bookstore/book/title How do I conduct the search? Sorry if this question is badly phrased but I am very confused.

I sort of understand it should go in a file like this(below) but then what should I do?

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

xml=loadXMLDoc("books.xml");
path="/bookstore/book/title"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);

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

</body>
</html>

I think you're confused :) Are you trying to just test out some Xpath that you've written to make sure it's getting the correct nodes? Most XML IDE suites have an XPath query tool built in that allows you open the document and write your xpath and run directly on the document. Things like Stylus Studio and Alotva XML Spy have this built right in.

I have no idea what you're doing with this HTML document about. This is a javascript that is using XML functions and Xpath to do something to that HTML document.

I think you're confused :) Are you trying to just test out some Xpath that you've written to make sure it's getting the correct nodes? .

No I'm trying to find out how I apply the Xpath to the XML document

Apply it in WHAT application? Are you running on your local machine? Are you trying to do this in javascrpt? Are you trying to do java or C#? How is your XML document being used?

You can only run a Xpath in some kind of environment context. It doesn't just magically happen. Whatever you're running it in has different ways to run it. So as I said, if you're trying to just test it on your machine, then you can do it whatever tool you're using to view and look at XML documents. (Like Stylus Studio or Altoa XML Spy). What application do you have the XML document open in? What are you viewing it with?

What are you viewing it with?

I'm using IDLE which is a Python editor.

You can't run an Xpath Query in IDLE. You need an XML IDE.

You can't run an Xpath Query in IDLE. You need an XML IDE.

Alles ist klar

Ja voll! Viel Gluek! Es tut mir leid, ich könnte nicht mehr hilfreich.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.