I have a problem with my XPath syntax I believe. I have been able to perform queries over XMLs quite easily so far, But now I stumble upon a problem, and it happens that when I have namespaces defined in my xmls, I can't search for xml tags in the 'regular' way.
let me detail the problem a little more; If I have an xml like this...
I have already implemented java code to succesfully make queries using XPath, like this...
Assume the code above works Fine as long as the XPath Expression is correct. (The Xpath expression is the first argument of the executeXpath method), But in this case it does Not work because I'm using a nameSpace. And I'm getting the following Exception:
javax.xml.transform.TransformerException: A node test that matches either NCName:* or QName was expected.
I don't know how to make it work, What am I missing?
Well, that isn't a correct XPath expression. You signify the namespace by using a prefix, just like you do in the document itself:
But that isn't all. You also have to somehow tell the XPath processor to associate the correct namespace URI with that prefix. I don't know anything about your "XPathToolkit" class, but it should be in there somewhere.
By the time I wrote the first post about this topic, I had already tried what you suggested, and unfortunately it didn't work. Let me show you some of the code I have inside my XPathToolkit class...
This method returns the result of any given XPath query, as a List of Nodes(from 0 to N).
Now, I have done my homework and, I have found the NamespaceContext interface, it supposedly allows you to do something like this
Given that NamespaceContext is an interface, I don't know how to implement it, or if there's an Object that implements this interface. Moreover, I don't see a setter method in the API that somehow would allow to configure the value of the Namespace for any given NamespaceContext Object.
There's nothing wrong with your (very simple) XPath expression. What's wrong must be how you're handling it.
Since the problem appears to be namespace-related, what happens if you remove all of the namespaces from the document and then run your code (without the namespace-related code)? I was doing something with XPath a couple of weeks ago and I couldn't get it to work by passing it a DOMSource, for example. The design of XPath is very sloppy, you get to pass "Object" to it in a lot of places and the documentation doesn't tell you what kind of Objects will work.
I have removed the namespaces(and used methods that don't implement namespace logic), and tried it in that way and it works of course. I did another experiment also; Since I have overloaded methods to perform XPath Queries, I included some child nodes in the original XML, but without namespaces, and I looked for them using a query, and it worked. The only case where it'll not work, is when the node is bounded by a namespace.
THE BIG PROBLEM is, that I can't remove the namespaces for a production environment, So.... I'm in kind of a Big trouble for what I can read in your last Reply.
I've been able to query your XML with your XPath expression using my own code, and my own NamespaceContext, so it can be done. Can you add some logging to your NamespaceContext implementation to make sure it's being called with the prefix you expect, and returning the correct namespace URI?
And what a good morning it is, when I have good news to report !
You were right! setting the Namespace awareness to true did the trick, the logging in the nameSpaceContext didn't even matter, and my InputSource was also correct all the time. Fortunately you figured it out. You saved me, you're my personal Jesus Christ, God Bless you.
I hope this knowledge helps other Ranchers in the Future.
You take good care, and seriously... Thank you ! You took the time, and that's something very few people do. Thanks for being selfless, and so much for others.
Have a nice day !
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop