ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

XML-INTO mapping an element with different attributes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML-INTO mapping an element with different attributes

    Hello,

    I have a problem trying to map an XML into a data structure using XML-INTO.

    There is an element in the message that can be a single value of can be an array containing multiple fields. I can create a data structure to EITHER retrieve the single value or a child-data structure to hold multiple fields. What I cannot do is a create a structure that will contain both.

    So before I create 2 data structures to hold the returned data (and run XML-INTO twice) can anyone define a data structure to work in a single pass?


    I am not good in working in abstract values to this is the part of the original XML that I am trying to map:
    <InterpretationVariable
    <Description>number of VK loans</Description>
    <DisplayName> VK Loans</DisplayName>
    <FieldName>Number of VK Loans</FieldName>
    <GroupName>General</GroupName>
    <Value i:type="b:int">0</Value>
    </InterpretationVariable>
    <InterpretationVariable>
    <Description>Amount and length of running loans</Description>
    <DisplayName>Running Loans</DisplayName>
    <FieldName>Running Agreements</FieldName>
    <GroupName>Running Loans</GroupName>
    <Value i:type="ArrayOfInterpretationRunningAgreement>
    <InterpretationRunningAgreement>
    <Type>RK</Type>
    <Amount>500</Amount>
    <Term>25</Term>

    and then multiple of the above fields ending with

    </InterpretationRunningAgreement>
    </Value>
    </InterpretationVariable>

    I can either define the <Value> as a sub-field of the InterpretationVariable data structure with a size attribute and the "0" will map. Or I can define <Value> as having a child data structure and that will map the array data into <InterpretationRunningAgreement>

    The problem is that both fields need to be defined as Value in the data structure:

    This
    D InterpretationVariableDS DS Qualified
    D Value 10a

    or

    D InterpretationVariableDS Qualified
    D Value LikeDS(ValuesDS)

    D ValueDS DS Qualified
    D InterpretationRunningAgreement LikeDS(InterpretationRunningAgreementDS) Dim(10)


    I do not want to move away from using XML-INTO as the XML contains a few thousand elements.

    Amy ideas?


    Thanks (and sorry for the spelling/spacing errors but I had to type all the values in as cut-and-paste was causing a 403 error)

    Alex





  • #2
    It is difficult to be sure as there are errors in your XML but I _think_ that what you need is the datasubf option.

    I wrote about it here: https://authory.com/JonParisAndSusan...INTO-Revisited

    You might also find the countprefix option useful for your nested arrays, or cases of optional elements. That is also described in the referenced article. You may be using that already but since you didn't post your XML-INTO code ...

    Assumedly you already have a namespace option in effect or you wouldn't be able to process the Value attributes at all.

    Comment


    • #3
      Click image for larger version

Name:	tempsnip.jpg
Views:	166
Size:	197.0 KB
ID:	158173

      Hopefully the real XML makes more sense!

      The xml element <InterpretationVariable><Value> can either be a single value (0 or 111148 in this example) or can also contain a child data structure of <InterpretationVariable><Value><InterpretationRunn ingAgreement><Soort> etc.


      So, is there a way of defining a single data structure to hold both definitions or do I need a separate data structure for each definition?

      Datasubf would allow me to access the "i:type" data contained within the <Value> tag but would it solve this issue?

      Thanks

      Alex





      Comment


      • #4
        Click image for larger version

Name:	tempsnip.png
Views:	168
Size:	76.1 KB
ID:	158176
        Attached Files

        Comment


        • #5
          Datasubf would allow me to access the "i:type" data contained within the <Value> tag but would it solve this issue?
          Did you read the piece? This is exactly what it is for.
          Code:
          Dcl-ds value;
             type char(20);
             data_value whateverDefinition;
          End-ds;
          That definition works for option { datasubf=data_} - if you don't like "data_" just change it to whatever makes sense to you.

          Comment


          • #6
            I don't think it's possible to use datasubf (or any other option) to handle both a simple value like 11148 and a data structure like ArrayOfInterpretationRunningAgreement.

            Comment


            • #7
              Hmmm - I was thinking of using nested DSs one for each possible array Barbara - but you are right that is not going to work.

              alex_rsa Can you supply the XML as text (attachment maybe) - the images you posted are pretty much useless in that I can't copy the text and "play" with the results. And I'm _not_ going to type it all in <grin> I _think_ I can see a way to d it by using multiple passes but ...
              Last edited by JonBoy; December 1, 2022, 05:03 PM.

              Comment


              • #8
                XML-SAX would handle it.

                Comment


                • #9
                  A different approach might be to use xslt to convert the xml to a delimited file, which can easily be handled. An example xslt to convert your xml to csv is attached.
                  Attached Files

                  Comment

                  Working...
                  X