Link to home
Start Free TrialLog in
Avatar of isvensen
isvensen

asked on

Can I extrapolate an element into multiple elements using xslt?

Hi,

I have an element in an xml that looks like this:

<forecast start_hr="1" start_int="1" end_hr="24" end_int="2" value="200"/>
Now I need that to be extrapolated to be:
<forecast start_hr="1" start_int="1" end_hr="1" end_int="1" value="200"/>
<forecast start_hr="1" start_int="2" end_hr="1" end_int="2" value="200"/>
<forecast start_hr="2" start_int="1" end_hr="2" end_int="1" value="200"/>
<forecast start_hr="2" start_int="2" end_hr="2" end_int="2" value="200"/>
<forecast start_hr="3" start_int="1" end_hr="3" end_int="1" value="200"/>
<forecast start_hr="3" start_int="2" end_hr="3" end_int="2" value="200"/>
<forecast start_hr="4" start_int="1" end_hr="4" end_int="1" value="200"/>
<forecast start_hr="4" start_int="2" end_hr="4" end_int="2" value="200"/>
...
<forecast start_hr="24" start_int="1" end_hr="24" end_int="1" value="200"/>
<forecast start_hr="24" start_int="2" end_hr="24" end_int="2" value="200"/>

Basically create an element for every half hour of the day in the given format. Of course, there's no guarantee that there's always 24 elements, so I need to dynamically use the end_hr and end_int of the original element like this (pseudocode)

iterations=(end_hr - start_hr +1) * 2;
if (start_hr = 2)  = iterations -1;
if (end_hr = 1)  = iterations -1;

prev_start_hr = start_hr;
prev_start_int = start_int;
for (i=1;i<iterations;i++)
{
<forecast start_hr=prev_start_hr start_int=prev_start_int end_hr=prev_start_hr end_int=prev_start_int value=value/>
if (prev_int = 1) prev_int = 2 else prev_hr++;
}

Any way I can do this in xslt?
Avatar of Badotz
Badotz
Flag of United States of America image

It might be possible, but it would be much simpler in script to just create the XML in a for...next loop.
Avatar of isvensen
isvensen

ASKER

The problem is that I receive this file from an external system, and we expect the format in the second way, but it comes in the first.
Is this scenario possible:

1) Receive the file
2) Refactor the file to meet your requirements
3) Submit the file to your application
No, unfortunately not. We request the file, receive it and want to load it into our application. The problem is we use a proprietary format for storing interval data, which requires us to have each interval between the start and end time. We actually submit the data to the external system like this (Market Operator in an energy market) but they provide us the queried data back in that format IF all the values are the same, which in some cases they are.

The only other solution would be to store the incoming data in a temporary table and then call a procedure of our proprietary language to extrapolate. But that means a datamodel change as well as additional code, so  I was hoping I could somehow do it with xslt.
It sounds like there *could* be a step in between "receiving" and "processing" - that is where the refactoring would come in.

Alas, I am no Michael Kay; an XSLT solution is far beyond my pathetic skills, sorry.
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again. I think that ticked off a last unknown (the recursive templates) and I think I understand it quite well... so next time I encounter a problem I shall try to abstain from your help!

Take care,

Ivar