• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Java xslt transform

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following xml
code:
i am trying to cut out the xml below using xpath
code:
here is my code
code:

my code is not prnting anything out. The nodes object is always null. What am i doing wrong?

 
Sheriff
Posts: 28328
96
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
Your XPath expression doesn't really look well-formed to me; that predicate after the "@" as part of a comparison doesn't look right. However even if you fixed it up, it looks to me like you're asking it to find nodes which have attributes named xmlns:i with a certain value. And that won't work because those things aren't attributes, they are namespace declarations.

If I'm right, then you're searching for that GetPatientTreatmentTeamResult element. Seems to me the easy way to do that would be to just search for an element named GetPatientTreatmentTeamResult. However that element is in a namespace whose URI is "urn:Custom-com:Common.2014.Services.Patient", so you'll want to make sure to use the getElementsByTagNameNS method.
 
Paul Clapham
Sheriff
Posts: 28328
96
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
Also, you should make sure your DocumentBuilder is namespace-aware. I believe that by default they aren't, but your DocumentBuilderFactory has a method to set the namespace-awareness mode on.
 
alex tribaka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What i would like to do is to get the xml i am looking for without using the node name GetPatientTreatmentTeamResult (this node will be different in different calls) and only using the namespace xmlns:i="http://www.w3.org/2001/XMLSchema-instance (all call have this) .
That's why i am opting for xpath. I also know that my xpath query is wrong. I am hoping to fix it.
 
Paul Clapham
Sheriff
Posts: 28328
96
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 can still use the getElementsByTagNameNS method; as the API documentation says:

localName - The local name of the elements to match on. The special value "*" matches all local names.



You want to get the first node which is in that namespace, right? That method will return all of them, but it returns them in document order so you can just use the first one from the list it returns.
 
Paul Clapham
Sheriff
Posts: 28328
96
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
And by the way, welcome to the Ranch!
 
alex tribaka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
node is still null

code:

Is this the way i should use doc.getElementsByTagNameNS?

Thanks for the welcome
 
Paul Clapham
Sheriff
Posts: 28328
96
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
Nope. As I said, you should use "*" for the local-name parameter.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...only using the namespace xmlns:i="http://www.w3.org/2001/XMLSchema-instance (all call have this)


I think relying on that namespace declaration, even all your instances have it, is a (very) bad choice. The reason is that its appearance has actually no functional purpose at all, it does not have any material purpose as far as the soap message you shown. Its presence would be not add any useful information to the message and its absence would not retire any information, neither. So you can expect at any time it might disappear or it might appear somewhere else... It is an unreliable piece of declaration to rely on.

When you want to "cut" out the fragment of xml you mentioned, it may be better to cut including its wrapping element (its parent) so that you get back a piece of xml itself capable of being treated as a complete document by itself if you want to. That means your cut would better including GetPatientTreatmentTeamResult or even simply including the GetPatientTreatmentTeamResponse. The latter is actually the starting place of the payload, the material info of the soap message...

I understand the name of GetPatientTreatmentTeamResponse may be varying but there must be some constance for the receiving end of the message to have a grip of it. That constance may be the namespace of the payload or something else...

I can send you a demo code block relying on the sole fact that the payload be the direct child of the s:Body using xpath. After getting the payload itself, and that if you want to use dom's getElementsByNS to look further into it, you have to know the namespace to firm up things. The approach may not be as simple as you might think if you have not much of the idea of what is all about. It shows you the basic tools with xpath as well as dom. But with the code block, I hope you may look up tutorials with a better idea to get the gist of what they want to show and demonstrate for the readers.

edition: Upon reading, I edit the sxpr that I've forgotten to append /* to it to keep the story I built consistent.
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic