Xml transformation question

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

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.

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.

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

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 :

<?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 :

<?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 (849 Bytes) outputxml.txt (575 Bytes)

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

Kind regrads,
Patrick

inputxml as picture
image

outputxml as picture
image

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

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 should help to clarify the difference between Javascript objects and JSON.

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 ?

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

A demo - Something like this...


image

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

Demo flow...

[{"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"]]}]

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 :

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

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

Thx,

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