Author |
Message
|
LH33 |
Posted: Thu Oct 30, 2003 10:13 am Post subject: Formatting output tags |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
I have a message flow that takes an input XML transaction and checks certain tags for values (bolded tags below). If the tags have values, I need to let them go to the outputroot unchanged. If the tags are not there, then I need to create an empty tag for the output root. My problem is that the tags are indexed. Can someone help me understand how to get my expected results shown below. I have included the input XML and what the desired XMl result should be. Thanks!! Lisa
Input XML when the tags are present:
<ChangeJob revision="1.0.0" environment="Test">
<DataArea>
<Change confirm="Always" fieldid="1"/>
<Job>
<Location>
<Address>
<Streets>
<Street index="1">
<Article>N</Article>
<Direction>NE</Direction>
<Type>AV</Type>
<Name>F 301 N PULASKI ST</Name>
</Street>
<Street index="2">
<Article>E</Article>
<Direction>E</Direction>
<Type>AV</Type>
<Name>N 222 Main street</Name>
</Street>
</Streets>
</Address>
</Location>
</Job>
</DataArea>
</ChangeJob>
Here is the XML when the tags are not present:
<ChangeJob revision="1.0.0" environment="Test">
<DataArea>
<Change confirm="Always" fieldid="1"/>
<Job>
<Location>
<Address>
</Address>
</Location>
</Job>
</DataArea>
</ChangeJob>
Here is the desired XML output when the input tags are missing:
<ChangeJob revision="1.0.0" environment="Test">
<DataArea>
<Change confirm="Always" fieldid="1"/>
<Job>
<Location>
<Address>
<Streets>
<Street index="1">
<Article></Article>
<Direction></Direction>
<Type></Type>
<Name></Name>
</Street>
<Street index="2">
<Article></Article>
<Direction></Direction>
<Type></Type>
<Name></Name>
</Street>
</Streets>
</Address>
</Location>
</Job>
</DataArea>
</ChangeJob>
Here is my current code:
DECLARE STREET1 CHAR;
SET STREET1 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.[]
as R where R.(XML.Attribute)index = '1');
DECLARE STREET2 CHAR;
SET STREET2 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.[]
as R where R.(XML.Attribute)index = '2');
DECLARE ARTICLE1 CHAR;
SET ARTICLE1 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[]
as R where R.(XML.Attribute)index = '1');
DECLARE ARTICLE2 CHAR;
SET ARTICLE2 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[]
as R where R.(XML.Attribute)index = '2');
DECLARE DIRECTION1 CHAR;
SET DIRECTION1 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction[]
as R where R.(XML.Attribute)index = '1');
DECLARE DIRECTION2 CHAR;
SET DIRECTION2 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction[]
as R where R.(XML.Attribute)index = '2');
DECLARE TYPE1 CHAR;
SET TYPE1 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type[]
as R where R.(XML.Attribute)index = '1');
DECLARE TYPE2 CHAR;
SET TYPE2 = THE ( SELECT ITEM R FROM InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type[]
as R where R.(XML.Attribute)index = '2');
IF STREET1 IS NULL or STREET1 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[1];
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[1].(XML.Attribute)index=1;
IF ARTICLE1 IS NULL or ARTICLE1 < ' ' THEN CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[1].(XML.Attribute)index=1;
IF DIRECTION1 IS NULL or DIRECTION1 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction[1].(XML.Attribute)index=1;
IF TYPE1 IS NULL or TYPE1 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type[1].(XML.Attribute)index=1;
END IF;
END IF;
END IF;
END IF;
IF STREET2 IS NULL or STREET2 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[2];
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[2].(XML.Attribute)index=2;
IF ARTICLE2 IS NULL or ARTICLE2 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[2].(XML.Attribute)index=2;
IF DIRECTION2 IS NULL or DIRECTION2 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Direction[2].(XML.Attribute)index=2;
IF TYPE2 IS NULL or TYPE2 < ' ' THEN
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Type[2].(XML.Attribute)index=2;
END IF;
END IF;
END IF;
END IF;
END IF;
Here is the output when I run this code:
<ChangeJob revision="1.0.0" environment="Test">
<DataArea>
<Change confirm="Always" fieldid="1"/>
<Job>
<Location>
<Address>
<Streets>
<Street index="1">
<Name></Name>
<Article index="1"/>
<Article index="2"/>
<Direction index="1"/>
<Direction index="2"/>
<Type index="1"/>
<Type index="2"/>
</Street>
<Street index="2"/>
</Streets>
</Address>
</Location>
</Job>
</DataArea>
</ChangeJob>
Thanks for any help!! |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Oct 30, 2003 11:01 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
You will probably have better luck if you create a reference to the appropriate Street element, and then work relative to that.
Then you won't have as many problems with creating elements in the wrong places. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Thu Oct 30, 2003 11:04 am Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
Would I create the reference to the inputroot tag and then if it wasn't there, create the output tag?
Thanks from another Baltimore MD WMQI user!!!! |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Oct 30, 2003 11:24 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
LH33 wrote: |
Would I create the reference to the inputroot tag and then if it wasn't there, create the output tag? |
Are you using 'Copy Entire Message'? Cause if so, I'd just check for the existance of the output tag, and then either set a reference to it or create a new field and then set the reference to that.
LH33 wrote: |
Thanks from another Baltimore MD WMQI user!!!! |
I thought those street names looked familiar.  _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Thu Oct 30, 2003 1:18 pm Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
Could you post a code sample here for your suggestion? I am using the following code and it is not working:
DECLARE searchArt REFERENCE TO OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[1];
DECLARE fieldFound BOOLEAN;
SET fieldFound = 'FALSE';
WHILE ((LASTMOVE(searchArt) = TRUE) AND (fieldFound = FALSE)) DO
IF searchArt.index = '1' THEN
RETURN TRUE;
ELSE
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[1].(XML.Attribute)index=1;
END IF;
END WHILE;
Thanks!!! |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Oct 30, 2003 1:47 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
LH33 wrote: |
Could you post a code sample here for your suggestion? I am using the following code and it is not working:
DECLARE searchArt REFERENCE TO OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[1];
|
If I understand your input XML correctly, it is only the Street elements that are indexed, and not the article element or the other child elements. So this should be OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[1], not ....Streets.Street.Article[1].
LH33 wrote: |
DECLARE fieldFound BOOLEAN;
SET fieldFound = 'FALSE';
WHILE ((LASTMOVE(searchArt) = TRUE) AND (fieldFound = FALSE)) DO
IF searchArt.index = '1' THEN
RETURN TRUE; |
I don't believe that RETURN is the right thing to use here. Unless you want the current compute node to stop executing and propogate the message to the Out terminal?
LH33 wrote: |
ELSE
CREATE FIELD OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article;
SET OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.Article[1].(XML.Attribute)index=1;
END IF;
END WHILE;
Thanks!!! |
But maybe we can get really tricky with the select function, and the coalesce function. Something sort of like
Code: |
set OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[1].* =
(SELECT COALESCE(R."Article",'') as "Article",
COALESCE(R."Direction",'') as "Direction",
COALESCE(R."Type",'') as "Type",
COALESCE(R."Name",'') as "Name" FROM
InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.[]
as R where R.(XML.Attribute)index = '1'); |
You'll probably have to play around with this a bit to get it to work properly. But the theory should be sound - use the Coalesce function to replace any null values with blank fields. But I haven't tried it. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Thu Oct 30, 2003 2:01 pm Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
You are right, the Street tag is the indexed tag. The Article, Direction, Type and Name tags occur once for each index:
<Streets>
<Street index="1">
<Article>N</Article>
<Direction>NE</Direction>
<Type>AV</Type>
<Name>F 301 N PULASKI ST</Name>
</Street>
<Street index="2">
<Article>N</Article>
<Direction>NE</Direction>
<Type>AV</Type>
<Name>F 301 N PULASKI ST</Name>
</Street>
</Streets>
If no tag exists in the input XML for the Article(both occurences), Direction(both occurences), Type(both occurences) and Name(just the 2nd ocurrence), I need to create an empty tag for the output. If the tag exists, then I just let the SET OutputRoot = InputRoot; take care of it. For instance, if all tags for street[1] are present, but no tags for street[2] are, I need to create empty tags for all street[2] tags.
If I understand the COALESCE function right, it will return the first null tag and in this example, I need to know all of them. Maybe I am misunderstanding the function
Thanks for any help!!!!!! |
|
Back to top |
|
 |
jefflowrey |
Posted: Thu Oct 30, 2003 7:28 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
LH33 wrote: |
If I understand the COALESCE function right, it will return the first null tag and in this example, I need to know all of them. Maybe I am misunderstanding the function
|
The coalesce function, as I understand it, returns the first NON-NULL argument that's passed to it. So if I do coalesce(NULL,NULL,NULL,...,NULL,'a'), it will return 'a'. Likewise, if I do coalesce('a',NULL,...,NULL) or coalesce('a','b','c',...), it will still return 'a'. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Fri Oct 31, 2003 6:08 am Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
Okay, now I understand! However, suppose my input XML looks like the following:
<Streets>
<Street index="1">
<Article>N</Article>
</Street>
<Street index="2">
<Type>AV</Type>
<Name>F 301 N PULASKI ST</Name>
</Street>
</Streets>
Where Direction, Type and Name are missing from the Street[1] set and the Article and Direction are missing from the Street[2] set. How would I traverse through to find all that are null from both Street[1] and Street[2] so I could create output tags for all that are null?
Sorry - this one is stumping me!!! Thanks!!!! Lisa |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Oct 31, 2003 6:28 am Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
Just loop over all the Street elements.
Code: |
declare streetCounter integer;
declare streetMax integer;
set streetMax = CARDINALITY(InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.*[]);
set streetCounter = 1;
while streetCounter < streetMax do
set OutputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street[streetCounter].* =
(SELECT COALESCE(R."Article",'') as "Article",
       COALESCE(R."Direction",'') as "Direction",
       COALESCE(R."Type",'') as "Type",
       COALESCE(R."Name",'') as "Name" FROM
InputRoot.XML.ChangeJob.DataArea.Job.Location.Address.Streets.Street.[]Â
as R where R.(XML.Attribute)index = streetCounter);
set streetCounter = streetCounter+1;
end while; |
The usual caveats about untested code and etc. Also notice that I'm only using the counter to create the appropriate output element, and not as an index to a particular input element. I'm using the Select statement to find the input element I'm interested in. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Fri Oct 31, 2003 6:56 am Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
Thanks!! I will try this and let you know! |
|
Back to top |
|
 |
LH33 |
Posted: Fri Oct 31, 2003 9:02 am Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
When I try to execute your code suggestion, I get a BIP2524 -
Any ideas or suggestions? |
|
Back to top |
|
 |
jefflowrey |
Posted: Fri Oct 31, 2003 12:06 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
LH33 wrote: |
When I try to execute your code suggestion, I get a BIP2524 -
Any ideas or suggestions? |
Try changing the OutputRoot...Street[streetCounter].* to OutputRoot....Street[streetCounter].*[] in the first set statement in the loop. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
LH33 |
Posted: Mon Nov 03, 2003 1:10 pm Post subject: |
|
|
Master
Joined: 21 Nov 2002 Posts: 200
|
Jeff,
I cannot get this to work for anything
Do you have any suggestions on what else I may try?
Thanks a lot for your help!!! Lisa |
|
Back to top |
|
 |
jefflowrey |
Posted: Mon Nov 03, 2003 3:50 pm Post subject: |
|
|
Grand Poobah
Joined: 16 Oct 2002 Posts: 19981
|
I'll see what I can figure out in the morning. _________________ I am *not* the model of the modern major general. |
|
Back to top |
|
 |
|