IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to handle XML subelements withe the same element names


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
rss0213

New User


Joined: 20 Jan 2010
Posts: 26
Location: Alabama

PostPosted: Mon Jan 14, 2013 7:47 am
Reply with quote

Hi. I have an XML schema I'm trying to parse that contains subelements with the same element name. For example:

Code:
/PaymentSegment/PaymentAmount/Currency
/PaymentSegment/PaymentAmount/Amount


... and ...

Code:
/PaymentSegment/InstructAmount/Currency
/PaymentSegment/InstructAmount/Amount


How do I distinguish between the two Currency and Amount fields for my MOVE statements?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Jan 14, 2013 8:19 am
Reply with quote

Hello,

If this is just beginning, suggest the element names be made unique.
Back to top
View user's profile Send private message
rss0213

New User


Joined: 20 Jan 2010
Posts: 26
Location: Alabama

PostPosted: Mon Jan 14, 2013 9:24 am
Reply with quote

The schema was created by a vendor, and it's used in the messages their application produces. Is there a way to distinguish them?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Jan 14, 2013 8:57 pm
Reply with quote

Hello,

One way would be to pre-process the messages and make sure the names are unique. Surely not too attractive.

If your organization is paying for this data/process, i suspect your management could influnce what was delivered - even if this was agreed on previously. . .

If this were my issue, i'd ask the vendor how other clients have dealt with the duplicate names.
Back to top
View user's profile Send private message
rss0213

New User


Joined: 20 Jan 2010
Posts: 26
Location: Alabama

PostPosted: Mon Jan 14, 2013 9:32 pm
Reply with quote

That's the approach I'm taking. Thanks

If anyone else knows how to handle this, please advise. Thanks
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Jan 14, 2013 10:07 pm
Reply with quote

The only way I can think of to handle this would be to set a flag in your COBOL program to indicate which branch of XML your program is currently processing; you would then know from the flag whether you have a PaymentAmount or InsttructAmount.
Back to top
View user's profile Send private message
Ed Goodman

Active Member


Joined: 08 Jun 2011
Posts: 556
Location: USA

PostPosted: Tue Jan 15, 2013 7:12 pm
Reply with quote

There is an 'END-OF-ELEMENT' event that throws when you get to the end of a tag. You should keep track of those and then you would know the level you are in.

The idea of making XML have unique names if not going to fly. It's got a hierarchy for exactly that reason.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Jan 15, 2013 8:03 pm
Reply with quote

Hello,

Guess who does not "do" XML . . . icon_redface.gif

Is there a Small document that explains why non-unique names woud be better than unique names? Or just from one's own knowledge/experience.

Most likely, i'll not be involved with XML directly/technically, but it is mentioned more these days than 5 years ago . . .

So i'll starting picking up bits here and there icon_cool.gif
Back to top
View user's profile Send private message
Ed Goodman

Active Member


Joined: 08 Jun 2011
Posts: 556
Location: USA

PostPosted: Tue Jan 15, 2013 9:40 pm
Reply with quote

www.w3schools.com/xml/xml_whatis.asp

It may be easiest to think of it as an IMS database.

There is a root, and different 'elements' hang under that, then each of those can have its own children ad infinitum.

So,
Code:
<case>
   <customer>
      <demographics>
          <name>Ed Goodman</name>
          <age>106</age>
      </demographics>
      <accounts>
          <checking>
               <account_num>123456789</account_num>
          </checking>
          <savings>
               <account_num>987654321</account_num>
          </savings>
      </accounts>
   </customer>
   <customer>
      <demographics>
          <name>Annie Goodman</name>
          <age>29</age>
      </demographics>
      <accounts>
          <checking>
               <account_num>123456789</account_num>
          </checking>
          <savings>
               <account_num>987654321</account_num>
          </savings>
      </accounts>
   </customer>
<case>
 

Notice how the 'account_num' tag is perfectly valid in both places? The normal reaction of relational DB folks is to try for account_num_checking and account_num_savings, but that's not really what you are representing. There might be any number of checking accounts, including none. Trying to do account_num_checking_01 .. 99 just makes things weird, and it wastes space.
Back to top
View user's profile Send private message
rss0213

New User


Joined: 20 Jan 2010
Posts: 26
Location: Alabama

PostPosted: Wed Jan 16, 2013 6:22 am
Reply with quote

I'm taking Mr Sample's approach. I set a flag that tells me which parent element I'm processing. I also explicitly turn OFF the other parent elements' flags that might have this same sub-element name. So, here's the code:

Code:
05  WS-FLAGS.
    10  WS-PMT-AMT-ELE           PIC  X(01)  VALUE 'N'.
        88  PMT-AMT-ELE                      VALUE 'Y'.
    10  WS-INS-AMT-ELE           PIC  X(01)  VALUE 'N'.
        88  INS-AMT-ELE                      VALUE 'Y'.

WHEN 'CONTENT-CHARACTERS'
    EVALUATE CURRENT-ELEMENT
        WHEN 'PAYMENTAMOUNT'
            MOVE 'Y'              TO  WS-PMT-AMT-ELE
            MOVE 'N'              TO  WS-INS-AMT-ELE
        WHEN 'INSTRUCTAMOUNT'
            MOVE 'Y'              TO  WS-INS-AMT-ELE
            MOVE 'N'              TO  WS-PMT-AMT-ELE
        WHEN 'CURRENCY'
            IF  PMT-AMT-ELE
                MOVE CURRENT-ELEMENT TO  PMT-AMT-CURRENCY
            ELSE
            IF  INS-AMT-ELE
                MOVE CURRENT-ELEMENT TO  INS-AMT-CURRENCY
            END-IF
            END-IF
        WHEN 'AMOUNT'
            IF  PMT-AMT-ELE
                MOVE CURRENT-ELEMENT TO  PMT-AMT-AMOUNT
            ELSE
            IF  INS-AMT-ELE
                MOVE CURRENT-ELEMENT TO  INS-AMT-AMOUNT
            END-IF
            END-IF
            MOVE CURRENT-ELEMENT  TO  PMT-AMT-AMOUNT


It turns out I have lots of these with this vendor's XML schema. Thanks for the help everyone!
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Capturing COBOL job and program names... All Other Mainframe Topics 2
No new posts Read file names from existing file th... DFSORT/ICETOOL 6
No new posts UUID4 - extracting time element COBOL Programming 1
No new posts Unable to retrieve Datasets Names usi... CLIST & REXX 20
No new posts Column names in SYSIBM tables DB2 5
Search our Forums:

Back to Top