Author |
Message
|
Vifferrider |
Posted: Mon Mar 02, 2015 5:23 am Post subject: CREATE NEXTSIBLING OF - I must be doing something wrong |
|
|
Newbie
Joined: 02 Mar 2015 Posts: 3
|
Hi
I am parsing an incoming XML that has a structure of
<OrderItems>
<OrderItem>
<OrderLineItemId>0</OrderLineItemId>
<ProductName>Product 1 Cert</ProductName>
</OrderItem>
<OrderItem>
<OrderLineItemId>1</OrderLineItemId>
<ProductName>Special Letter 2</ProductName>
</OrderItem>
<OrderItem>
<OrderLineItemId>2</OrderLineItemId>
<ProductName>Info Pack 3</ProductName>
</OrderItem>
</OrderItems>
and am using (an abridge version of the code)
Code: |
DECLARE rItems REFERENCE TO InputRoot.XMLNSC.*:Order.*:OrderItems.*;
DECLARE rLines REFERENCE TO OutputRoot;
DECLARE LineCount INT;
SET LineCount=0;
CREATE FIELD OutputRoot.XMLNSC.order.lines AS rLines;
WHILE LASTMOVE(rItems) DO
SET LineCount = LineCount + 1;
SET rLines.line.lineno = LineCount;
SET rLines.line.ourdescription = rItems.ProductName;
SET rLines.line.yourstockcode = '';
SET rLines.line.yourdescription = '';
SET rLines.line.uom = '';
CREATE NEXTSIBLING OF rLines AS rLines REPEAT;
MOVE rItems NEXTSIBLING REPEAT TYPE N AME;
END WHILE; |
I am getting
<lines>
<line>
<lineno>1</lineno>
<ourdescription>Product 1 Cert</ourdescription>
<yourstockcode></yourstockcode>
<yourdescription></yourdescription>
<uom></uom>
</line>
</lines>
<lines>
<line>
<lineno>2</lineno>
<ourdescription>Special Letter 2</ourdescription>
<yourstockcode></yourstockcode>
<yourdescription></yourdescription>
<uom></uom>
</line>
</lines>
<lines>
<line>
<lineno>3</lineno>
<ourdescription>Info Pack 3</ourdescription>
<yourstockcode></yourstockcode>
<yourdescription></yourdescription>
<uom></uom>
</line>
</lines>
<lines/>
and expect it to be along the structure of
<lines>
<line>
Blah
Blah
</line>
<line>
Blah
Blah
</line>
<line>
Blah
Blah
</line>
</lines>
Please can you advise what I am doing wrong >
thanks
[/code] |
|
Back to top |
|
 |
Vitor |
Posted: Mon Mar 02, 2015 6:42 am Post subject: Re: CREATE NEXTSIBLING OF - I must be doing something wrong |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Vifferrider wrote: |
Please can you advise what I am doing wrong > |
Your CREATE should be at the start not the end. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
joebuckeye |
Posted: Mon Mar 02, 2015 7:39 am Post subject: |
|
|
 Partisan
Joined: 24 Aug 2007 Posts: 365 Location: Columbus, OH
|
The line
Quote: |
CREATE NEXTSIBLING OF rLines AS rLines REPEAT; |
is creating a new entry of <lines> which does not appear to be what you want. |
|
Back to top |
|
 |
Vifferrider |
Posted: Mon Mar 02, 2015 9:47 am Post subject: Re: CREATE NEXTSIBLING OF - I must be doing something wrong |
|
|
Newbie
Joined: 02 Mar 2015 Posts: 3
|
Vitor wrote: |
Vifferrider wrote: |
Please can you advise what I am doing wrong > |
Your CREATE should be at the start not the end. |
I have done this but still not right
Now I get
<lines>
<line>
Blah
Blah
</line>
</lines>
<lines>
<line>
Blah
Blah
</line>
</lines>
<lines>
<line>
Blah
Blah
</line>
</lines> |
|
Back to top |
|
 |
joebuckeye |
Posted: Mon Mar 02, 2015 9:56 am Post subject: |
|
|
 Partisan
Joined: 24 Aug 2007 Posts: 365 Location: Columbus, OH
|
Your code is creating a new <lines> element each time through your loop.
The CREATE statement inside your loop should not be there. |
|
Back to top |
|
 |
Vifferrider |
Posted: Mon Mar 02, 2015 2:06 pm Post subject: |
|
|
Newbie
Joined: 02 Mar 2015 Posts: 3
|
Sussed it, with thanks to the pointers given
Code: |
WHILE LASTMOVE(rItems) DO
SET LineCount = LineCount + 1;
SET rLines.line[LineCount].lineno = LineCount;
SET rLines.line[LineCount].ourdescription = rItems.ProductName;
SET rLines.line[LineCount].yourstockcode = '';
SET rLines.line[LineCount].yourdescription = '';
SET rLines.line[LineCount].unitprice = rItems.ItemTotal;
SET rLines.line[LineCount].uom = '';
MOVE rItems NEXTSIBLING REPEAT TYPE NAME;
END WHILE;
|
Thanks all |
|
Back to top |
|
 |
adubya |
Posted: Tue Mar 03, 2015 2:20 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
Vifferrider wrote: |
Sussed it, with thanks to the pointers given
Code: |
WHILE LASTMOVE(rItems) DO
SET LineCount = LineCount + 1;
SET rLines.line[LineCount].lineno = LineCount;
SET rLines.line[LineCount].ourdescription = rItems.ProductName;
SET rLines.line[LineCount].yourstockcode = '';
SET rLines.line[LineCount].yourdescription = '';
SET rLines.line[LineCount].unitprice = rItems.ItemTotal;
SET rLines.line[LineCount].uom = '';
MOVE rItems NEXTSIBLING REPEAT TYPE NAME;
END WHILE;
|
Thanks all |
Using arrays is an inefficient way of traversing message structures and should be avoided where possible.
How about:-
Code: |
DECLARE rItems REFERENCE TO InputRoot.XMLNSC.*:Order.*:OrderItems.*;
DECLARE rLines, rLine REFERENCE TO OutputRoot;
DECLARE LineCount INT;
SET LineCount=0;
CREATE FIELD OutputRoot.XMLNSC.order.lines AS rLines;
WHILE LASTMOVE(rItems) DO
CREATE LASTCHILD OF rLines AS rLine NAME 'line'
SET LineCount = LineCount + 1;
SET rLine.lineno = LineCount;
SET rLine.ourdescription = rItems.ProductName;
SET rLine.yourstockcode = '';
SET rLine.yourdescription = '';
SET rLine.uom = '';
MOVE rItems NEXTSIBLING REPEAT TYPE NAME;
END WHILE; |
(typed in blind, not tested ) |
|
Back to top |
|
 |
joebuckeye |
Posted: Tue Mar 03, 2015 7:06 am Post subject: |
|
|
 Partisan
Joined: 24 Aug 2007 Posts: 365 Location: Columbus, OH
|
Yes, time for refactoring now to make it better.
I would ditch the WHILE and use FOR. The FOR statement simplifies the setup for looping through items.
Code: |
DECLARE rLines, rLine REFERENCE TO OutputRoot;
DECLARE LineCount INT;
SET LineCount=0;
CREATE FIELD OutputRoot.XMLNSC.order.lines AS rLines;
FOR itemsRef AS InputRoot.XMLNSC.*:Order.*:OrderItems[] DO
CREATE LASTCHILD OF rLines AS rLine NAME 'line'
SET LineCount = LineCount + 1;
SET rLine.lineno = LineCount;
SET rLine.ourdescription = itemsRef .ProductName;
SET rLine.yourstockcode = '';
SET rLine.yourdescription = '';
SET rLine.uom = '';
END FOR;
|
Again, this is blind typed in, not tested. |
|
Back to top |
|
 |
adubya |
Posted: Tue Mar 03, 2015 8:00 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
|
Back to top |
|
 |
|