web stats
XML transformer - Mirth Community

Go Back   Mirth Community > Mirth Connect > Development

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2019, 01:52 AM
Adrien Adrien is offline
Mirth Newb
 
Join Date: Mar 2019
Posts: 9
Adrien is on a distinguished road
Default XML transformer

I created a channel.

In this source channel (file reader), I get back my XML file which is in a folder.
Here is a part of the file :

<execute>
<date>2019-03-05</date>
<heure>11:33:00</heure>
</execute>
<NABMs>...</NABMs>

With a transformer I would like to add this :

<prestataire><medecins><medecin><numeroAdeli>12345 </numeroAdeli>
<identification><code>AAA</code><libelle>BBB</libelle></identification>
</medecin></medecins>
<uniteFonctionnelle><code>789</code><libelle>YYY</libelle>
</uniteFonctionnelle>
</prestataire>

between the tag : </execute> and <NABMs>

In Destination transformer I create step : type -> Javascript
I dont know how to code what I want. ( I never develop on mirth and
I know a little about JavaScript)

I need help coding my task!

Thank you in advance to any one who may be able to give me some ideas.

Adrien
Reply With Quote
  #2  
Old 05-22-2019, 09:12 AM
collinsmj collinsmj is offline
OBX.1 Kenobi
 
Join Date: Jun 2014
Location: Southern USA
Posts: 27
collinsmj is on a distinguished road
Default

I would be happy to assist you in your problem. There are many ways to do what you want. I couldn't possible cover all of them. I will past the code from my message transformer that results in your desired message.

#Note:
I had to immediately edit my reply to include the following:
I am using Mirth Connect 3.5.2.
The notation used to alter the msg body in Mirth Connect is E4X. E4X is a rather old javascript library. This code will not run in a regular javascript engine. Knowing only a little javascript isn't an issue. A larger issue is you are new to E4X and it's syntax. But this will help you. It takes a bit of time to learn the odd bits and catches.
#end note



I started with an input XML message of

Code:
  <root>
    <execute>
        <date>2019-03-05</date>
        <heure>11:33:00</heure>
    </execute>
    <NABMs>stuff here</NABMs>
</root>
All xml messages have a root node. You didn't post what your message's root node was in your example so I am assuming <root>.


We can now transform the message in a number of ways.

This will add a blank node after the <execute>...</execute> block. please note I use the += operator. This would be equivalent to msg[execute] = msg[execute] + new XML(...);
Code:
  msg['execute'] += new XML("<prestataire/>");
This code defines your raw XML and assigns it to a variable. Which is then used to replace an xmlnode. ValueToBeReplaced will not appear in the input or output xml. This is just to prove a point of understanding how the assignment works.
Code:
var a = <medecins>
    <medecin>
      <numeroAdeli>12345</numeroAdeli>
    </medecin>
  </medecins>;
  
msg['prestataire']['ValueToBeReplaced'] = a;

The following will declare a new XML variable and assign values to child nodes. The child nodes are created during runtime when the values are assigned to them. Then we assign the entire XML node to a location in the message.
Code:
var b = new XML('<identification></identification>');
b['code'] = 'AAA';
b['libelle'] = 'BBB';

msg['prestataire']['medecins']['medecin']['numeroAdeli'] += b;
Or we could just ignore the XML brackets and formating rules. We just create the entire structure with these two assignment statements. UniteFonctionnelle, code, or libelle do not exist until the child values are assigned.
Code:
 
msg['prestataire']['uniteFonctionnelle']['code'] = 789;
msg['prestataire']['uniteFonctionnelle']['libelle'] = 'YYY';

The result is the following.

Code:
<root>
    <execute>
        <date>2019-03-05</date>
        <heure>11:33:00</heure>
    </execute>
    <prestataire>
        <medecins>
            <medecin>
                <numeroAdeli>12345</numeroAdeli>
                <identification>
                    <code>AAA</code>
                    <libelle>BBB</libelle>
                </identification>
            </medecin>
        </medecins>
        <uniteFonctionnelle>
            <code>789</code>
            <libelle>YYY</libelle>
        </uniteFonctionnelle>
    </prestataire>
    <NABMs>stuff here</NABMs>
</root>

Take the code blocks one by one and add them to your transformer. Run a test each time and you will see how each block modifies the message.

Happy coding!

Last edited by collinsmj; 05-22-2019 at 09:18 AM. Reason: added a note
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 09:46 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation