web stats
Mirth Community - View Single Post - XML transformer
Thread: XML transformer
View Single Post
  #2  
Old 05-22-2019, 10: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 10:18 AM. Reason: added a note
Reply With Quote