Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

XML Simple Transformation - programatically setting tag "ID=..." attribute

bryan_sippel3
Explorer
0 Likes
3,033

I have a requirement to populate an XML file from ABAP using

CALL TRANSFORMATION zpa_cpr_to_xml

         SOURCE root = lt_cpr_xml

         RESULT XML lw_xml.

where lt_cpr_xml is an ABAP deep structure (nested internal tables)

    and lw_xml is the XML string returned by the transformation.

I believe most of it is working properly by I still need to set the ID=... attribute on one of the resulting XML tags.

The sample file provided for my requirement includes the following section of XML, including the attribute ID="123456" on the CPR:name node:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

     ...

  <CPR:payrollInfo>

     ...

    <CPR:employees>

      <CPR:employee>

        <CPR:name id="123456">John Smith</CPR:name>

     ...

Currently, my corresponding XML simple transformation looks like this...

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT"/>

  <tt:template>

    <CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

      <tt:loop name="cprdata" ref=".ROOT">

     ...

        <CPR:payrollInfo>

     ...

          <CPR:employees>

            <tt:loop name="employee" ref="$cprdata.payinfo.payemps">

              <CPR:employee>

                <CPR:name id="123456">

                  <tt:value ref="$employee.empname"/>

                </CPR:name>

     ...

I need to set the ID="123456" attribute on the CPR:name tag, replacing the "123456" constant with the value associated with the $employee.empid variable as it loops through my $cprdata.payinfo.payemps source root deep structure that was passed into the transformation.

What tt:instruction do I use to set attributes of particular XML nodes to values from the ABAP source data?

I have had a tough time finding any documentation or examples regarding this requirement, so any assistance would be greatly appreciated.

Thanks,

Bryan

1 ACCEPTED SOLUTION
Read only

bryan_sippel3
Explorer
0 Likes
1,576

Solution:

After changing my root node to explicitly reference my underlying ABAP table type, it was able to see the new EmpID field I had added to the structure...

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT" type="ddic:ZPA_CPR_XML_TAB"/>

  <tt:template name="ZPA_CPR_XML">

    <CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

      <tt:loop name="cprdata" ref=".ROOT">

     ...

        <CPR:payrollInfo>

     ...

...and I was then able to use the tt:attribute command

     ...

          <CPR:employees>

            <tt:loop name="employee" ref="$cprdata.payinfo.payemps">

              <CPR:employee>

                <CPR:name>

                  <tt:attribute name="id" value-ref="$employee.empid"/>

                  <tt:value ref="$employee.empname"/>

                </CPR:name>

     ...

...which produced the resulting XML


          <CPR:employees>

              <CPR:employee>

                <CPR:name id="EmpID">EmpName</CPR:name>

     ...

I hope that helps whoever else is trying to do this.

Thanks for your interest.

Bryan

I have a requirement to populate an XML file from ABAP using

CALL TRANSFORMATION zpa_cpr_to_xml

         SOURCE root = lt_cpr_xml

         RESULT XML lw_xml.

where lt_cpr_xml is an ABAP deep structure (nested internal tables)

    and lw_xml is the XML string returned by the transformation.

I believe most of it is working properly by I still need to set the ID=... attribute on one of the resulting XML tags.

The sample file provided for my requirement includes the following section of XML, including the attribute ID="123456" on the CPR:name node:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

     ...

  <CPR:payrollInfo>

     ...

    <CPR:employees>

      <CPR:employee>

        <CPR:name id="123456">John Smith</CPR:name>

     ...

Currently, my corresponding XML simple transformation looks like this...

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT"/>

  <tt:template>

    <CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

      <tt:loop name="cprdata" ref=".ROOT">

     ...

        <CPR:payrollInfo>

     ...

          <CPR:employees>

            <tt:loop name="employee" ref="$cprdata.payinfo.payemps">

              <CPR:employee>

                <CPR:name id="123456">

                  <tt:value ref="$employee.empname"/>

                </CPR:name>

     ...

I need to set the ID="123456" attribute on the CPR:name tag, replacing the "123456" constant with the value associated with the $employee.empid variable as it loops through my $cprdata.payinfo.payemps source root deep structure that was passed into the transformation.

What tt:instruction do I use to set attributes of particular XML nodes to values from the ABAP source data?

I have had a tough time finding any documentation or examples regarding this requirement, so any assistance would be greatly appreciated.

Thanks,

Bryan

1 REPLY 1
Read only

bryan_sippel3
Explorer
0 Likes
1,577

Solution:

After changing my root node to explicitly reference my underlying ABAP table type, it was able to see the new EmpID field I had added to the structure...

<?sap.transform simple?>

<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">

  <tt:root name="ROOT" type="ddic:ZPA_CPR_XML_TAB"/>

  <tt:template name="ZPA_CPR_XML">

    <CPR:eCPR xmlns:CPR="http://www.dir.ca.gov/dlse/CPR-Prod-Test/CPR.xsd">

      <tt:loop name="cprdata" ref=".ROOT">

     ...

        <CPR:payrollInfo>

     ...

...and I was then able to use the tt:attribute command

     ...

          <CPR:employees>

            <tt:loop name="employee" ref="$cprdata.payinfo.payemps">

              <CPR:employee>

                <CPR:name>

                  <tt:attribute name="id" value-ref="$employee.empid"/>

                  <tt:value ref="$employee.empname"/>

                </CPR:name>

     ...

...which produced the resulting XML


          <CPR:employees>

              <CPR:employee>

                <CPR:name id="EmpID">EmpName</CPR:name>

     ...

I hope that helps whoever else is trying to do this.

Thanks for your interest.

Bryan