Author |
Message
|
recallsunny |
Posted: Wed Aug 17, 2005 11:46 am Post subject: Detaching the Child Branches from a Tree |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
Allright, this is a very dumb question, but help me find the best way to achieve this:
My OutputTree structure
Code: |
<RootTag>
<elementa>a</elementa>
<elementb>b</elementb>
<elementa>c</elementa>
<Chld1>
<chldele1></chldele1>
</Chld1>
<Chld1>
<chldele2></chldele2>
</Chld1>
</RootTag>
|
I need to remove all those child elements from the OutputTree and it should look like:
Code: |
<RootTag>
<elementa>a</elementa>
<elementb>b</elementb>
<elementa>c</elementa>
</RootTag>
|
I've tried various combinations of Move -- Detach statements, but I end with up with atleast one Child.
 |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Aug 17, 2005 12:56 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Don't use CopyEntireMessage. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
recallsunny |
Posted: Wed Aug 17, 2005 1:19 pm Post subject: |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
You are correct, But there are about a zillion fields I need to Copy from the InputRoot to OutputRoot. Is there a better way to just copy the Parent elements and ignore all the child tags....?
 |
|
Back to top |
|
 |
jefflowrey |
Posted: Wed Aug 17, 2005 3:02 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Do a search through here, and find a sample of a loop that uses anonymous references to walk the InputRoot.<Parser> subtree.
Insert an if statement that checks the FIELDNAME to see if it is something you do not want to copy. Do not copy it to OutputRoot if it is. Copy it to OutputRoot if it is not. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
recallsunny |
Posted: Thu Aug 18, 2005 9:31 am Post subject: A work around! |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
Ok, Here is the Solution:
I could've done as Jeff suggested by looping through all the fields and adding them if it doesn't belong to Child Tag and omit the other Child tags. The Only downside of this is, my parent elements by far outnumber the Child Tags...resulting in execution of Loop many times before it starts omitting the child tags.
So I decide the smart way to eliminate the Looping a zillion times is to detach "the few" Child tags that get added after a CopyEntireMessage, saving me some CPU cycles(hopefully).
Code: |
DECLARE outCursor REFERENCE TO InputRoot.<parserName>.<rootTagName>;
X : WHILE LASTMOVE(outCursor) DO
DECLARE inCursor REFERENCE TO outCursor;
MOVE inCursor NEXTSIBLING NAME 'Chld1';
IF (LASTMOVE(inCursor) = FALSE) THEN
LEAVE X;
ELSE
DETACH inCursor;
END IF;
MOVE outCursor NEXTSIBLING;
END WHILE X;
|
The Math works out well if the number of Child Tags are far less than the Parent elements itself (Out of a 150 Parents elements I had only 4 child tags).... You could do the math!
Cheers  |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Aug 18, 2005 9:34 am Post subject: Re: A work around! |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
recallsunny wrote: |
You could do the math! |
And you'd end up with 4 more steps than if you'd done it my way...
Because you'd already CopyEntireMessage, which does a tree copy. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
recallsunny |
Posted: Thu Aug 18, 2005 10:27 am Post subject: |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
Aint Tree Copy more efficient than While Loop ????  |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Aug 18, 2005 10:31 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
recallsunny wrote: |
Aint Tree Copy more efficient than While Loop ????  |
Erm.
Not necessarily.
I can't speak for sure if
Code: |
Set OutputRoot = InputRoot |
is more efficient than a while loop that did the same thing.
But regardless, you are walking the tree twice, in your code. Once to copy the message, and then once to prune the message.
Also, the code you posted seemed to try to modify InputRoot. I assume that was a typo, as it wouldn't work for real. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
recallsunny |
Posted: Thu Aug 18, 2005 10:51 am Post subject: |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
Corrections: Change InputRoot --> OutputRoot
After all the hard work, I just noticed that unlike-parser-copy always does the Tree Copy......uughh!
Code: |
Performing tree copy of 'inRef.ReqLine[lineOut].*:*[1]' to 'outRef.msg_PO_Req[lineOut].POREQLINE[1].*:*[1] |
Leaves me no choice..... |
|
Back to top |
|
 |
recallsunny |
Posted: Fri Aug 19, 2005 7:15 am Post subject: |
|
|
 Disciple
Joined: 15 Jun 2005 Posts: 163 Location: Massachusetts
|
jefflowrey wrote: |
Do a search through here, and find a sample of a loop that uses anonymous references to walk the InputRoot.<Parser> subtree.
|
I couldn't find much here, so let me add a few lines that worked for me!
Code: |
DECLARE inCursor REFERENCE TO InputRoot.<blahblah>
DECLARE outCursor REFERENCE TO OutputRoot.<blahblahblah>
X : WHILE LASTMOVE(inCursor) DO
MOVE inCursor NEXTSIBLING;
--** Check for ReqDistribution Child Tags
IF (FIELDNAME(inCursor)) = 'Child1' THEN
ITERATE X;
END IF;
SET outCursor.{FIELDNAME(inCursor)} = FIELDVALUE(inCursor);
END WHILE X;
|
Cheers  |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Aug 19, 2005 7:21 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Code: |
DECLARE inCursor REFERENCE TO InputRoot.<blahblah>
DECLARE outCursor REFERENCE TO OutputRoot.<blahblahblah>
WHILE LASTMOVE(inCursor) DO
MOVE inCursor NEXTSIBLING;
--** Check for ReqDistribution Child Tags
IF (FIELDNAME(inCursor)) <> 'Child1' THEN
SET outCursor.{FIELDNAME(inCursor)} = FIELDVALUE(inCursor);
END IF;
END WHILE X;
|
But that's just me.
It will have to get more complicated, with additional loops and moves and stuff, if one needs to filter an entire tree, instead of a tree at a particular level. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
|