Creating XML with same tag different attribute name

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
ggarze
Premium Member
Premium Member
Posts: 78
Joined: Tue Oct 11, 2005 9:37 am

Creating XML with same tag different attribute name

Post by ggarze »

I'm used to creating xml's with predefined tags for example
<contact>
<firstname>Joe</firstname>
<lastname>Smith</lastname>
</contact>

but this latest is different. The tag is generic and uses and attribute or label so the above needs to be:
<contact>
<contactField name="firstname">Joe</contactField>
<contactField name="lastname">Smith</contactField>
</contact>

The problem is in my output I get this
<contact>
<contactField name="firstname">Joe Smith</contactField>
</contact>

every value shows up one after the other before the tag closes instead of having <tag></tag> for each value.

So this is how I have it set up in the transformer to XML stage under the 'Description' column of the output.
/contact/contactField/@name
/contact/contactField/text()
/contact/contactField/@name
/contact/contactField/text()

seems like the stage won't close the <contactField> tag. I have a whole bunch more values like this to create but can't figure out why it's doing this.

Thanks
eostic
Premium Member
Premium Member
Posts: 3838
Joined: Mon Oct 17, 2005 9:34 am

Post by eostic »

this is fairly poorly designed xml. The better design is where the metadata is truly the names of the tags, as in your first example.

What happens here is that the structure of the xml is totally different. The whole "contact name" is no longer the repeating element....instead, contactField is the repeating element.

So...that means you have to have "n" rows for as many contactFields as you might want.

Do a pivot so that first name, last name, etc. etc. etc. are on their own rows, and then it will work for you, with contactField selected as "key".

Ernie
Ernie Ostic

blogit!
<a href="https://dsrealtime.wordpress.com/2015/0 ... ere/">Open IGC is Here!</a>
rameshrr3
Premium Member
Premium Member
Posts: 609
Joined: Mon May 10, 2004 3:32 am
Location: BRENTWOOD, TN

Post by rameshrr3 »

A dirty way would use a transformer stage variable to concatenate string tags +elements data et all in a large string and pass it as an XML data element to an XML ouput stage. I faced a similar issue a few years back , not with one , but 2 attributes whose value kept changing for differnt columns in the input record , mind you it was an FpML spec we were trying to comply with :roll: . I think there are ways to do it if you create an intermediate data set , but as eostic says , this a crazy type of XML that I hope not to contend with in the near future. Maybe DS 8.5 supports such an output , but you never know .
ggarze
Premium Member
Premium Member
Posts: 78
Joined: Tue Oct 11, 2005 9:37 am

Post by ggarze »

In the end what we did was sequence each tag to get to a normal XML like so:
<contact>
<contactField1 name="firstname">Joe</contactField1>
<contactField2 name="lastname">Smith</contactField2>
<contactField3 ...
</contact>

Then we feed the XML through another job doing a Change() function replacing contactField1 with contactField, contactField2 with contactfield and so on. What a pain.

I understood what Ernie was saying but the problem I could understand there was that all my tags weren't just <contactField name=>. Along with those tags I also had <customField name= customValue= > along with others so i think that would caused issue.

rameshrr3 i understand what you did too but if a tag had no value you still have that open and close tag showing in the XML right unless within that stage variable for every tag you first inpected the input field value. If that was the case that would have been some hairy code.
rameshrr3
Premium Member
Premium Member
Posts: 609
Joined: Mon May 10, 2004 3:32 am
Location: BRENTWOOD, TN

Post by rameshrr3 »

I used the stage variables only for the elements which were qualified by changing attribute value for the same attribute.
This prebuilt XMl fragment was cast as an XML data element to an XML output stage , which also received other columns under differnt tags from transformer. Target App was willing to tolerate empty tags. Tag supression can still be achieved by concatenating null value data with tag strings and applying NullToEmpty ( over the whole string of start tag, element and end tag ) , so that the rest doesnt go to Null .
The rest of the elements were transformed to XML the standard way ( Xpaths and XML ouptput stage ) . The stage variable did look like hairy code , but it worked . I could not use the logic suggested by eostic ( pivot attrib vlues ) only because the source app did not send the attribute values to be used in qualifying data.
Post Reply