xml need help to calculate unit weight

Posted by: sygui

xml need help to calculate unit weight - 06/11/21 08:55 AM

For each Line segment, I need to find the unit weight: LineWeight/Quantity and put the answer at Unitweight. Can you help ?

<Line>
<Quantity>1.00</Quantity>
<ItemNumber>TOP63661</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>50.00000</LineWeight>
</Line>
<Line>
<Quantity>10.00</Quantity>
<ItemNumber>JET502128</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>0.08800</LineWeight>
</Line>
<Line>
<Quantity>10.00</Quantity>
<ItemNumber>JET502174</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>0.10000</LineWeight>
</Line>
Posted by: MartinS

Re: xml need help to calculate unit weight - 06/14/21 11:49 AM

so you need to calculate UnitWeight = LineWeight * Quantity and put the result back to the data file?
Posted by: sygui

Re: xml need help to calculate unit weight - 06/14/21 02:49 PM

I need to divide LineWeight per Quantity and put the results in Unitweight
Posted by: MartinS

Re: xml need help to calculate unit weight - 06/15/21 12:17 PM

you could try with splitting at <Line>, doing the calculation and afterwards replacing <UnitWeight/> with the result of your calculation: <UnitWeight>#result#</UnitWeight>
Posted by: sygui

Re: xml need help to calculate unit weight - 06/15/21 03:05 PM

Martin, the problem is that I don't have any idea of how to calculate this with a script
Posted by: Jean-Cédric

Re: xml need help to calculate unit weight - 06/15/21 03:39 PM

Just a thought but if you intend to use the modified data in a form, you could do the calculation in it...no?
Posted by: sygui

Re: xml need help to calculate unit weight - 06/16/21 07:15 AM

Jean-Cedric, ok but how do I calculate it ?
Posted by: MartinS

Re: xml need help to calculate unit weight - 06/16/21 08:08 AM

It's possible to do that in Workflow, but it needs a lot of steps.

First you need to add lines to your raw data file to add a root node (if not already being present), e.g.:
<data>

</data>
This could be done with two Add/Remove Text plugins.

Next you should have a Change Emulation plugin to mark your data to be XML.

Then use a XML Splitter to split on nodes <Lines>.

Next step is a Mathematical Operations plugin which extracts the xml fields for the operation needed and put the result in a job info variable, e.g. %9.

Afterwards get a Search and Replace plugin to replace <UnitWeight/> by <UnitWeight>%9</UnitWeight>

Next following two Add/Remove Text steps to remove the unncessary header of the xml split file and also remove the last line which is the root node closing tag.

And finally have a Send to Folder plugin with option "Concatenate files" enabled and Separator string \r\012.

That's it.
Posted by: Jean-Cédric

Re: xml need help to calculate unit weight - 06/16/21 11:21 AM

Well in your form, you need to setup a custom data selection with the following code:
Code:
=floattostr((strtofloat(XmlGet('/Lines[1]/Line['+inttostr(&current.line)+']/LineWeight[1]'))/strtofloat(XmlGet('/Lines[1]/Line['+inttostr(&current.line)+']/Quantity[1]'))))


My guess is that you will use Repeat in which case, that explains the use of inttostr(&current.line). If you do not need repeat, then the code line should be like this:
Code:
=floattostr((strtofloat(XmlGet('/Lines[1]/Line[1]/LineWeight[1]'))/strtofloat(XmlGet('/Lines[1]/Line[1]/Quantity[1]'))))


You should look at these documentation link in order to understand the code:

=floattostr((strtofloat(XmlGet('/Lines[1]/Line[1]/LineWeight[1]'))/strtofloat(XmlGet('/Lines[1]/Line[1]/Quantity[1]'))))

and this as well line repeat

Of course, this code is good for this specific XML that I created, based on what you have provided:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Lines>
<Line>
<Quantity>1.00</Quantity>
<ItemNumber>TOP63661</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>50.00000</LineWeight>
</Line>
<Line>
<Quantity>10.00</Quantity>
<ItemNumber>JET502128</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>0.08800</LineWeight>
</Line>
<Line>
<Quantity>10.00</Quantity>
<ItemNumber>JET502174</ItemNumber>
<ProductDescription/>
<UnitWeight/>
<CustomChar1/>
<CustomChar2>74</CustomChar2>
<CustomChar3>T W L</CustomChar3>
<CustomNumeric1/>
<LineWeight>0.10000</LineWeight>
</Line>
</Lines>
Posted by: sygui

Re: xml need help to calculate unit weight - 06/17/21 11:14 AM

I will try this, thks