Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to perform an XPath Query with namespaces.

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdi there !

first thing's first, Happy new year to everyone!

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?

Please Help me,

Sincerely,

Jose


 
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul, and Good morning to everyone.

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.

I'm lost in this part, Could you please help me?

or.... somebody Out there: HELP ! ! ! :lol:

Sincerely,

Jose
 
Paul Clapham
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You implement it just like you would implement any other interface: you write code for all of the required methods. The specs are in the API documentation so just start coding:
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there Paul!

Thanks for the last reply, it seems that I'm always a step ahead of you
I figured out that interface and Built the implementation class. Now I have an overloaded method, that looks like this:



Where NamespaceContextImpl is the Object that implements NamespaceContext correctly.

Now, Given the XML I attached yesterday...



I'm still NOT able to perform the Xpath Query correctly, Here's my code....



Could you be so kind to tell me What am I doing wrong? I know I'm close to get it working. Please HELP ME !

Thanks in advance,

Jose
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I give up, I have done every example on the internet, and I'm still unsuccessful.

Please help me out. I strongly suspect that my problem lies on my XPath expression.

Thanks in advance as always.

I'll read you later. I need to take some air.

Best Regards,

Jose
 
Paul Clapham
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul !

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.

What can I do?

I fear for my life...... :?
 
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

My test code is very simple:
 
Carey Evans
Ranch Hand
Posts: 225
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something else occurred to me... if you're using DocumentBuilder, are you sure you're parsing the namespaces? DocumentBuilder.setNamespaceAware() defaults to false, but XPath needs it set to true.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good morning Carey !

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 !

Sincerely,

Jose
 
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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic