# Xml transformation question

**URL:** <https://discourse.nodered.org/t/xml-transformation-question/44866>\
**Category:** General\
**Created:** [28 April 2021 13:56 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866 "2021-04-28T13:56:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [28 April 2021 13:56 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/1 "2021-04-28T13:56:28Z")

</div>

Hi,  
I'am a newbie in Node-red and I have a simple question.  
I receive an XML file on a windows file share and I need to transform this xml to another layout and put it on another windows share?

What is the best way to do this ? Are there functions available ?

In xml :

\<?xml version="1.0" encoding="utf-8"?\> VANAS 2021-04-21T13:10:08 001 STUKNUMMER1 10 002 STUKNUMMER2 22 

Output Xml :

\<?xml version="1.0" encoding="UTF-8"?\>

\<DI\_TELEGRAM\>

 VANAS 2021-04-21T13:10:08 001 STUKNUMMER1 pcs 10 002 STUKNUMMER2 pcs 22

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 April 2021 15:26 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/2 "2021-04-28T15:26:21Z")

</div>

Welcome to the forum @PDH

Start by feeding it through an XML node and then into a debug node and see what that makes of it. The XML node is one of the core nodes, it should be available in the palette.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [28 April 2021 19:17 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/3 "2021-04-28T19:17:09Z")

</div>

> [@PDH](#):
>
> In xml :
> 
> \<?xml version="1.0" encoding="utf-8"?\> VANAS 2021-04-21T13:10:08 001 STUKNUMMER1 10 002 STUKNUMMER2 22

As this input string is not valid XML, I doubt you will be able to process it with the `xml` node...

If what you are really trying to do is "fix up" someone else's bad xml data and pass it along, I would first try to see if the bad xml output could be fixed.

Otherwise, if all you need to do is add the appropriate `<DI_TELEGRAM>` ... `</DI_TELEGRAM>` tags around that input string, you could use any of a number of Javascript functions to insert the tags inside the incoming string. Most of us would use a `function` node with some substring() or regex processing -- but I think the `string` or `change` nodes can also do some minor string replacements.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 April 2021 20:13 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/4 "2021-04-28T20:13:51Z")

</div>

Ah yes, I understand the question now. I should have looked more carefully at the non-xml input.

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [29 April 2021 05:39 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/5 "2021-04-29T05:39:19Z")

</div>

Hi ,  
Sorry for the bad xml strings, seems the cut/paste didn't work well.  
second try. (also attached the files).  
I would like to transform the xml (thru something like you woild do with xslt )  
Or do i first make a josn object and then convert the to antoher json object and then back to xml ?  
**Input xml :**

```auto
<?xml version="1.0" encoding="utf-8"?>
<AcknowledgeDeliveryReceipt>
  <ApplicationArea>
    <Sender>
      <LogicalId>VANAS</LogicalId>
    </Sender>
    <CreationDateTime>2021-04-21T13:10:08</CreationDateTime>
  </ApplicationArea>
  <DataArea>
    <DeliveryReceipt>
      <ReceiptLine>
        <LineNumber>001</LineNumber>
        <ItemIds>
          <ItemId>
            <Id>STUKNUMMER1</Id>
          </ItemId>
        </ItemIds>
        <ReceiptQuantity uom="pcs">10</ReceiptQuantity>
      </ReceiptLine>
	  <ReceiptLine>
        <LineNumber>002</LineNumber>
        <ItemIds>
          <ItemId>
            <Id>STUKNUMMER2</Id>
          </ItemId>
        </ItemIds>
        <ReceiptQuantity uom="pcs">22</ReceiptQuantity>
      </ReceiptLine>
    </DeliveryReceipt>
  </DataArea>
</AcknowledgeDeliveryReceipt>

```

**output/xml :**

```auto
<?xml version="1.0" encoding="UTF-8"?>
<DI_TELEGRAM>
<HEADER> 
	<FULL>
		<HEADER_SOURCE>VANAS</HEADER_SOURCE>
		<HEADER_DATE>2021-04-21T13:10:08</HEADER_DATE>
	</FULL>
</HEADER>
<BODY>
	<RECEIPT>
		<LineId>001</LineId>
		<Item_itemNo>STUKNUMMER1</Item_itemNo>
		<Item_baseQtyUnit_id>pcs</Item_baseQtyUnit_id>
		<Item_Qty>10</Item_baseQtyUnit_id>
	</RECEIPT>
	<RECEIPT>
		<LineId>002</LineId>
		<Item_itemNo>STUKNUMMER2</Item_itemNo>
		<Item_baseQtyUnit_id>pcs</Item_baseQtyUnit_id>
		<Item_Qty>22</Item_baseQtyUnit_id>
	</RECEIPT>
</BODY>
</DI_TELEGRAM>

```

[inputxml.txt](https://discourse.nodered.org/uploads/short-url/eoRGZeWCw02XQn6YVHGvBJDgWzu.txt) (849 Bytes) [outputxml.txt](https://discourse.nodered.org/uploads/short-url/l6Pav5nmyyonWNpGH5wvjVfHGiB.txt) (575 Bytes)

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [29 April 2021 05:40 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/6 "2021-04-29T05:40:31Z")

</div>

Hi ,  
Seems that copying xml node into the reply does,'t work.  
So the attachment should be ok.

Kind regrads,  
Patrick

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [29 April 2021 05:44 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/7 "2021-04-29T05:44:09Z")

</div>

inputxml as picture  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/7/17d961ce77726d24af78bbcf6718930030a5f11e.png)

outputxml as picture  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/403fee02dd6fbced97ff019d4fb2da2a9e9e6b7e.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 April 2021 09:00 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/8 "2021-04-29T09:00:19Z")

</div>

> [@PDH](#):
>
> Seems that copying xml node into the reply does,'t work.

You have to put XML between lines containing triple backticks to stop the forum interpreting it as Markdown. I have edited your previous post so it shows ok.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 April 2021 09:01 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/9 "2021-04-29T09:01:49Z")

</div>

> [@PDH](#):
>
> I would like to transform the xml (thru something like you woild do with xslt )  
> Or do i first make a josn object and then convert the to antoher json object and then back to xml ?

It is up to you, whichever technique you are happiest with. I would convert it to an object and back again but that is because I am more familiar with javascript.

By the way they aren't JSON objects, (which are strings) they are javascript objects.

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [30 April 2021 09:43 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/10 "2021-04-30T09:43:11Z")

</div>

I created a new object and put i thru the xml node to create an XML string.  
This was succesfull but i need to change the node to something like "Orders" .  
Is this possible thru the XML node ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 April 2021 09:53 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/11 "2021-04-30T09:53:10Z")

</div>

> [@PDH](#):
>
> This was succesfull but i need to change the node to something like "Orders" .

Change what? Can you explain further. Maybe you could also share a demo flow (using inject node to inject sample XML data)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [30 April 2021 11:19 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/12 "2021-04-30T11:19:12Z")

</div>

> [@Steve-Mcl](#):
>
> using inject node to inject sample XML data

A demo - Something like this...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7b5ab17f73f571ed2b059321b608c198013cb185.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f101e05313400db049f014359842201e0c9e5b2.png)

then explain what you mean by wanting `something like "Orders"` ?

Demo flow...

```auto
[{"id":"c74cbb31.a50f68","type":"inject","z":"167a2e98.ad02c1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":760,"y":320,"wires":[["3d35deba.27d3c2"]]},{"id":"3d35deba.27d3c2","type":"template","z":"167a2e98.ad02c1","name":"XML Input","field":"payload","fieldType":"msg","format":"html","syntax":"mustache","template":"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<AcknowledgeDeliveryReceipt>\n <ApplicationArea>\n <Sender>\n <LogicalId>VANAS</LogicalId>\n </Sender>\n <CreationDateTime>2021-04-21T13:10:08</CreationDateTime>\n </ApplicationArea>\n <DataArea>\n <DeliveryReceipt>\n <ReceiptLine>\n <LineNumber>001</LineNumber>\n <ItemIds>\n <ItemId>\n <Id>STUKNUMMER1</Id>\n </ItemId>\n </ItemIds>\n <ReceiptQuantity uom=\"pcs\">10</ReceiptQuantity>\n </ReceiptLine>\n <ReceiptLine>\n <LineNumber>002</LineNumber>\n <ItemIds>\n <ItemId>\n <Id>STUKNUMMER2</Id>\n </ItemId>\n </ItemIds>\n <ReceiptQuantity uom=\"pcs\">22</ReceiptQuantity>\n </ReceiptLine>\n </DeliveryReceipt>\n </DataArea>\n</AcknowledgeDeliveryReceipt>","output":"str","x":920,"y":320,"wires":[["55130e93.e46c4"]]},{"id":"55130e93.e46c4","type":"xml","z":"167a2e98.ad02c1","name":"","property":"payload","attr":"","chr":"","x":1090,"y":320,"wires":[["955b7c5f.f811e","dc136248.1e61b"]]},{"id":"955b7c5f.f811e","type":"debug","z":"167a2e98.ad02c1","name":"Input XML as JS object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1300,"y":320,"wires":[]},{"id":"dc136248.1e61b","type":"function","z":"167a2e98.ad02c1","name":"Transform","func":"function makeReceipt(lineId, itemNo, baseQtyUnit_id, qty) {\n return {\n \"LineId\": [lineId],\n \"Item_itemNo\": [itemNo],\n \"Item_baseQtyUnit_id\": [baseQtyUnit_id],\n \"Item_Qty\": [qty]\n }\n}\nfunction makeHeader(src, date) {\n return {\n \"HEADER_SOURCE\": [src],\n \"HEADER_DATE\": [date]\n }\n}\n\nconst telegram = {\n \"DI_TELEGRAM\": {\n \"HEADER\": [\n {\n \"FULL\": []\n }\n ],\n \"BODY\": [\n {\n \"RECEIPT\": []\n }\n ]\n }\n}\n\nlet src = msg.payload.AcknowledgeDeliveryReceipt.ApplicationArea[0].Sender[0].LogicalId[0];\nlet dt = msg.payload.AcknowledgeDeliveryReceipt.ApplicationArea[0].CreationDateTime[0];\ntelegram.DI_TELEGRAM.HEADER[0].FULL.push(makeHeader(src, dt));\n\nvar receipts = msg.payload.AcknowledgeDeliveryReceipt.DataArea[0].DeliveryReceipt[0].ReceiptLine;\nreceipts.forEach(function(rec) {\n let lineId = rec.LineNumber[0];\n let itemNo = rec.ItemIds[0].ItemId[0].Id[0];\n let baseQtyUnit_id = rec.ReceiptQuantity[0].$.uom;\n let qty = rec.ReceiptQuantity[0]._;\n telegram.DI_TELEGRAM.BODY[0].RECEIPT.push(makeReceipt(lineId, itemNo, baseQtyUnit_id, qty));\n})\n\nmsg.payload = telegram;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":900,"y":400,"wires":[["3bf25969.2ec406"]]},{"id":"64e2e5e2.60a9ec","type":"debug","z":"167a2e98.ad02c1","name":"Transformed XML","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1290,"y":400,"wires":[]},{"id":"3bf25969.2ec406","type":"xml","z":"167a2e98.ad02c1","name":"","property":"payload","attr":"","chr":"","x":1090,"y":400,"wires":[["64e2e5e2.60a9ec"]]}]

```

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [30 April 2021 11:30 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/13 "2021-04-30T11:30:46Z")

</div>

Hi ,  
Yes , something like that.  
My mistake to call "Orders" when should have been "DI\_TELEGRAM".  
Can you send me then content of your "Transform" node and parameters for the XML node out ?  
I always get below output :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/41e23cd1f8a06a022c8739741fc154e61d64175d.png)

Is that "root" I want to change.  
Thx in advance ,  
Patrick

---

<div class="post-metadata">

**Author:** ![PDH](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@PDH](https://discourse.nodered.org/u/PDH)\
**Post date:** [30 April 2021 11:34 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/14 "2021-04-30T11:34:32Z")

</div>

Hi ,  
found content(looked over it). Imported the flow.

Thx,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 June 2021 11:34 UTC](https://discourse.nodered.org/t/xml-transformation-question/44866/15 "2021-06-29T11:34:56Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
