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

XML transformation from one format to another

 
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I'm trying to transform a XML from a format to another one.
I already have the java code to create the Java Object, given a XML input text, even with the help of XSD (both for validation and Jaxb class generation).
This is the input format:



I would like to have the following XML as output:


Before doing something wrong, I would like to know what is the best practice to achieve this.
This should be done for every xml field in the XML that may have many other fields at different levels.


Thanks in advance
 
Saloon Keeper
Posts: 15484
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should probably do this using an XSLT. This is what they were designed to do.

Java provides an XSLT processor in it's JAXP API. While I personally think JAXP is horrible, I think that's a direct result from the fact that working with XML in general is horrible.

https://en.wikipedia.org/wiki/XSLT
https://docs.oracle.com/javase/tutorial/jaxp/xslt/transformingXML.html
 
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


Before doing something wrong, I would like to know what is the best practice to achieve this.


If you talk about xml and related technologies, xslt is the appropriate enabling technology to do that. But you application must be in a position that can naturally merge xslt into it. Otherwise, any other way to do that is not even close.

I guess the world has so much dreadful things happened and happening, and for that much long in time, that it's got reflected into the programming community as well. The wide-spread indignation towards so many things facilitates some people raising their hate speech or asserting their "faith" without knowing the "matter itself" and getting carried away with "words all prefabricated". That's too bad.
 
Francesco Bellini
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Stephan, this is the first time for me with XSLT and that is the information I was looking for.
Kind Regards
 
Francesco Bellini
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Stephan suggestions I've finally found the solution.
Just in case this could help someone with my same problem, I want to post the XSL I've used for the purpose:



Notice that I renamed the field 2222222222 with AB1 and so on.
So this is the sample input:


And this is the expected result:
 
g tsuji
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
That amounts to practically rewrite a xml just like writing in a text editor with the original print by your side... Any minute change to the original xml necessitates a restart the rewrite... That is not how one appreciates xslt as a technology, quite the contrary.

If you want to tap into the gist and the power of xslt, I can show you one. It is not that simple. If one does not understand xslt at all, that may look even complicated (h6e? you say whatever you want). If you really have no intention to tap into it, I leave you in your comfort zone.
 
Francesco Bellini
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot master!

Well, I totally agree with you.
I had the suspect there was some way to avoid writing all those lines (approximately 1000 in my case), but it was my first xsl. So thanks a lot for that.

Anyway, I am going to study your perfect solution.

Thanks!
 
Francesco Bellini
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

how can I modify the xsl if the code contains some parameter?

I mean something like this:


That should be translated to
 
g tsuji
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
That is a sample not specific enough to define the need in case where ABn node have both text content and an ID attribute. I make an assumption that it will be treated as that in the previous requirement, namely, it is treated the same disregarded the existence of ID attribute. However, if the text content of ABn is empty and that it exists an ID attribute, it will be treated differently, displaying the ID in the path and its value in the resultant text content of the mapping element.

I can show you this last time the full detail of how it can be done. (The path will exhibit @ID rather than plain ID, if you understand what path means in the xpath language.) The only changes appear in the template with match (see below), the rest can be kept the same.

With that much of the details full of instructive hints, I would leave you to sort any other requirements out yourself for your own good.

Edit:
Upon reviewing, I decide to make two amendments to it.
1) Obviously a typo missing a slash in select="'/@ID'".
2) Take out the parent::* test so that the script would extend its scope of applicability allowing only BODY tag then nothing else (ie only root level).
 
Francesco Bellini
Ranch Hand
Posts: 42
Oracle Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!

I've just made an adjustment at row 14, removing the @.
Now I think it's perfect!


Many thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic