So, why do I have no replies?
a) Nobody knows the answer b) It's been asked too many times before c) The answer is so obvious it's embarrasing d) Nobody likes me ;-(
Jon
Jonathan Melhuish a écrit :
I'm trying to do something essentially fairly straightforward but I can't for the life of me see why it's not working :-( All I want to do is retreive certain nodes from within my XML database, eg. the "title" nodes.
As far as I understand, using the Xpath query "//title" should select all of the title nodes, irrespective of their position in the document tree - and indeed it does, when I use the Xindice command-line interface, it gives me exactly what I want. But when I use the XPathQueryService from within a Java program it doesn't, it returns everything, including the parent node and all of it's children!
That's obvious, I hear you cry, "you've cocked up the Java code"! But the same code, when fed, for instance "/", returns everything. Or when fed a blank query, it returns nothing. Which seems sensible.
What gives?
TIA,
Jon
PS: Just a nudge in the right direction would be helpful. I've also attatched the relevant code sections just in case you still suspect them ;-)
------------------------------------------------------------------------
From the Java source:
XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService","1.0");
ResourceSet resultSet = service.query(xpath);
ResourceIterator i = resultSet.getIterator();
while (i.hasMoreResources()) {
XMLResource r = ((XMLResource) i.nextResource());
Element doc = ((Document) r.getContentAsDOM()).getDocumentElement();
if (doc.getFirstChild() != null) {
String s = doc.getFirstChild().getNodeValue(); // text node
results.add(doc.getAttributeNS("http://xml.apache.org/xindice/Query","key"));
}
}
// now eliminate dupes
Iterator listIterator = results.iterator();
while (listIterator.hasNext()) {
String curElement = (String) listIterator.next();
int index = results.indexOf(curElement);
// is there another element identical to this one? if so then remove the current element if (results.indexOf(curElement) != results.lastIndexOf(curElement)) listIterator.remove();
}
return results;
Call from the JSP file:
List results = manager.querySelective("//title"); Iterator i = results.iterator(); while (i.hasNext()) { TerrainMap map = manager.getMap((String) i.next()); String str = Utils.serializeDocument(map.saveToXML()); str = str.replaceFirst("<\\?xml version=\"1.0\"\\?>",""); // eliminate the first processing instruction out.println(str);
-- Jonathan Melhuish, E101 Distributed Technology Group Advanced Processing Centre QinetiQ Malvern
email: [EMAIL PROTECTED] Tel: 01684 894121 Fax: 01684 894389
This e-mail may contain information which is commercial-in-confidence and/or legally privileged. For persons other than the intended recipient(s), any use, disclosure, copying or distribution of the e-mail or information contained therein is prohibited.