Extract certain values from XML

This should be a simple problem to solve but I'm not good with code so I've come up stuck

I have an XML that i'm converting using the json node to extact vertain values using a function node.

I want to get just the type, cost and colour of each object and convert back to xml.

<?xml version="1.0" standalone="yes"?>
<Products>
  <Product>
    <Type>one</Type>
    <Cost>2</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>heavy</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>two</Type>
    <Cost>5</Cost>
    <Quantity>10</Quantity>
	<Size>medium</Size>
	<Weight>light</Weight>
	<Colour>green</Colour>
   </Product>
     <Product>
    <Type>three</Type>
    <Cost>3</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>heavy</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>four</Type>
    <Cost>12</Cost>
    <Quantity>10</Quantity>
	<Size>large</Size>
	<Weight>heavy</Weight>
	<Colour>yellow</Colour>
   </Product>
     <Product>
    <Type>five</Type>
    <Cost>25</Cost>
    <Quantity>10</Quantity>
	<Size>small</Size>
	<Weight>light</Weight>
	<Colour>red</Colour>
   </Product>
     <Product>
    <Type>six</Type>
    <Cost>25</Cost>
    <Quantity>18</Quantity>
	<Size>medium</Size>
	<Weight>light</Weight>
	<Colour>blue</Colour>
   </Product>
</Products>
  

I have a function which will give me the type and this is where I'm stuck, how to format my function to return type, cost and colour?

for (let i = 0; i < msg.payload.Products.Product.length; i++)

{
    
msg.payload.Products.Product[i] = [msg.payload.Products.Product[i].Type[0]];

}

return msg;

Thanks

Here is one way...

const data = msg.payload.Products.Product
for (let index = 0; index < data.length; index++) {
    const Product = data[index];
    data[index] = {
        Type: Product.Type,
        Cost: Product.Cost,
        Colour: Product.Colour,
    }
}
return msg;

Flow...

[{"id":"2fad0bd21ed94ffd","type":"inject","z":"49f61d916c8f6022","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":1490,"y":100,"wires":[["bfd4939520419f99"]]},{"id":"bfd4939520419f99","type":"template","z":"49f61d916c8f6022","name":"original XML","field":"payload","fieldType":"msg","format":"handlebars","syntax":"plain","template":"<?xml version=\"1.0\" standalone=\"yes\"?>\n<Products>\n  <Product>\n    <Type>one</Type>\n    <Cost>2</Cost>\n    <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>red</Colour>\n   </Product>\n     <Product>\n    <Type>two</Type>\n    <Cost>5</Cost>\n    <Quantity>10</Quantity>\n\t<Size>medium</Size>\n\t<Weight>light</Weight>\n\t<Colour>green</Colour>\n   </Product>\n     <Product>\n    <Type>three</Type>\n    <Cost>3</Cost>\n    <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>red</Colour>\n   </Product>\n     <Product>\n    <Type>four</Type>\n    <Cost>12</Cost>\n    <Quantity>10</Quantity>\n\t<Size>large</Size>\n\t<Weight>heavy</Weight>\n\t<Colour>yellow</Colour>\n   </Product>\n     <Product>\n    <Type>five</Type>\n    <Cost>25</Cost>\n    <Quantity>10</Quantity>\n\t<Size>small</Size>\n\t<Weight>light</Weight>\n\t<Colour>red</Colour>\n   </Product>\n     <Product>\n    <Type>six</Type>\n    <Cost>25</Cost>\n    <Quantity>18</Quantity>\n\t<Size>medium</Size>\n\t<Weight>light</Weight>\n\t<Colour>blue</Colour>\n   </Product>\n</Products>","output":"str","x":1640,"y":100,"wires":[["468a8c5864382d4c"]]},{"id":"468a8c5864382d4c","type":"xml","z":"49f61d916c8f6022","name":"","property":"payload","attr":"","chr":"","x":1790,"y":100,"wires":[["7dfd99eabe79f7c2","1cb607b178bc43a9"]]},{"id":"7dfd99eabe79f7c2","type":"function","z":"49f61d916c8f6022","name":"Keep Type, Cost & Colour","func":"const data = msg.payload.Products.Product\nfor (let index = 0; index < data.length; index++) {\n    let Product = data[index];\n    data[index] = {\n        Type: Product.Type,\n        Cost: Product.Cost,\n        Colour: Product.Colour,\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1590,"y":160,"wires":[["4fb306fb0e154988"]]},{"id":"1cb607b178bc43a9","type":"debug","z":"49f61d916c8f6022","name":"before","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":100,"wires":[]},{"id":"4fb306fb0e154988","type":"xml","z":"49f61d916c8f6022","name":"","property":"payload","attr":"","chr":"","x":1790,"y":160,"wires":[["7464e8e3b370722e"]]},{"id":"7464e8e3b370722e","type":"debug","z":"49f61d916c8f6022","name":"after","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":160,"wires":[]}]

Steve thank you so much that works perfectly.

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