cancel
Showing results for 
Search instead for 
Did you mean: 

XML Generator Template Issues

mike_korcynski
New Contributor III

I’m trying to use a XML Generating template to bind some JSON to XML. It looks like it should work to me but it never binds correctly. I’ve simplified the template down just to the piece that is failing. The input JSON looks like :

[{"import-record":{"@type-name":"mentoring","native":{"field":[{"@type":"person-list","@name":"c-advisee","people":[{"person":{"last-name":"Ouyang","first-names":"Mengyao"}},{"person":{"last-name":"Liu","first-names":"Jingyi"}},{"person":{"last-name":"Giannakakis","first-names":"Georgios"}},{"person":{"last-name":"Cao","first-names":"Sufeng"}},{"person":{"last-name":"Fiedler","first-names":"Jacob S."}}]}]}}}]

And my template looks like:

<?xml version='1.0' encoding='UTF-8'?>
<import-record type-id="$import-record.at-type-id" type-name="$import-record.at-type-name">
    <native>#foreach($field in $import-record.native.field)
        <field name="$field.at-name">
            <people>#foreach($person in $field.people.person)
                <person>
                    <links>#foreach($link in $person.links.link)
                        <link type="$link.at-type" privacy="$link.at-privacy" href="$link.at-href" id="$link.at-id"/>#end
                    </links>
                    <last-name>$person.last-name</last-name>
                    <initials>$person.initials</initials>
                    <first-names>$person.first-names</first-names>
                    <addresses>#foreach($address in $person.addresses.address)
                        <address type="$address.at-type" iso-country-code="$address.at-iso-country-code">#foreach($line in $address.line)
                            <line type="$line.at-type">$line.value</line>#end
                            <grid href="$address.grid.at-href" longitude="$address.grid.at-longitude" latitude="$address.grid.at-latitude" id="$address.grid.at-id"/>
                        </address>#end
                    </addresses>
                    <email-address>$person.email-address</email-address>
                    <identifiers>#foreach($identifier in $person.identifiers.identifier)
                        <identifier scheme="$identifier.at-scheme">$identifier.value</identifier>#end
                    </identifiers>
                </person>#end
            </people>
        </field>#end
    </native>
</import-record>

Pipeline sequence is:

00

The output out of XML Generator is:

"xml": "<?xml version="1.0" encoding="UTF-8"?><import-record type-name="mentoring"><native> <field name="c-advisee"><people> </people></field> </native></import-record>"

I don’t understand given the template and the data why the values are not populating?

5 REPLIES 5

robin
Former Employee

I haven’t tried this yet but on first look this line, <people>#foreach($person in $field.people.person), I would expect to be <people>#foreach($person in $field.people) to iterate on the people array.

robin
Former Employee

@mike.korcynski, yes there were a few small changes to make.

For the provided JSON above, this was the template that I believe produces the desired XML format (see further below):

<?xml version="1.0" encoding="UTF-8"?>
<import-record type-id="$import-record.at-type-id" type-name="$import-record.at-type-name">
   <native>
      #foreach($field in $import-record.native.field)
      <field name="$field.at-name">
         <people>
            #foreach($person in $field.people)
            <person>
               <links>
                  #foreach($link in $person.person.links)
                  <link type="$link.at-type" privacy="$link.at-privacy" href="$link.at-href" id="$link.at-id" />
                  #end
               </links>
               <last-name>$person.person.last-name</last-name>
               <initials>$person.person.initials</initials>
               <first-names>$person.person.first-names</first-names>
               <addresses>
                  #foreach($address in $person.person.addresses)
                  <address type="$address.at-type" iso-country-code="$address.at-iso-country-code">
                     #foreach($line in $address.line)
                     <line type="$line.at-type">$line.value</line>
                     #end
                     <grid href="$address.grid.at-href" longitude="$address.grid.at-longitude" latitude="$address.grid.at-latitude" id="$address.grid.at-id" />
                  </address>
                  #end
               </addresses>
               <email-address>$person.person.email-address</email-address>
               <identifiers>
                  #foreach($identifier in $person.person.identifiers)
                  <identifier scheme="$identifier.at-scheme">$identifier.value</identifier>
                  #end
               </identifiers>
            </person>
            #end
         </people>
      </field>
      #end
   </native>
</import-record>

resulting in the following output (pretty-printed):

<?xml version="1.0" encoding="UTF-8"?>
<import-record type-name="mentoring">
   <native>
      <field name="c-advisee">
         <people>
            <person>
               <last-name>Ouyang</last-name>
               <first-names>Mengyao</first-names>
            </person>
            <person>
               <last-name>Liu</last-name>
               <first-names>Jingyi</first-names>
            </person>
            <person>
               <last-name>Giannakakis</last-name>
               <first-names>Georgios</first-names>
            </person>
            <person>
               <last-name>Cao</last-name>
               <first-names>Sufeng</first-names>
            </person>
            <person>
               <last-name>Fiedler</last-name>
               <first-names>Jacob S.</first-names>
            </person>
         </people>
      </field>
   </native>
</import-record>

The $person.person usage is required because the input JSON has a people array containing objects which we assign to the $person variable in the foreach). These objects in turn contain a field person whose value is another JSON object.

mike_korcynski
New Contributor III

I’ve simplified my case down a bit do show here, I may file a bug as the template was generated by the Snap against a valid XSD schema, which is why I was surprised when it did not work. Thanks for the follow up.

Hi Robin,
I’m also trying to generate xml out of an xsd, but getting the error every time. Is some other setting that needs to be done to populate the xml?
Thanks for reply