0

I'm using SAP Simple Transformation and I want to set value of unitCode attribute from ABAP field which is defined inside my structure. Let's say it's UNITCODE field.

 <cbc:InvoicedQuantity tt:value-ref="INVOICEDQUANTITY" unitCode="C62" unitCodeListID="UNECRec20"/>

Right now unitCode is hardcoded as value C62 but I want that this attribute takes value from ABAP UNITCODE field (in same structure as INVOICEDQUANTITY). How can I make this happen?

Thanks in advance!

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
TyRRRax
  • 55
  • 11
  • 1
    I removed `xslt` from the title and from the tags, because your question is about SAP Simple Transformation, not XSLT. – Sandra Rossi Oct 23 '20 at 17:01

2 Answers2

3

You may initialize an attribute from an ABAP variable using tt:attribute:

  <cbc:InvoicedQuantity tt:value-ref="INVOICEDQUANTITY" unitCodeListID="UNECRec20">
    <tt:attribute name="unitCode" value-ref="UNITCODE"/>
  </cbc:InvoicedQuantity>

Below is a Minimal Reproducible Example:

  • ABAP :
    DATA : BEGIN OF ls_data,
             invoicedquantity TYPE decfloat34,
             unitcode         TYPE string,
           END OF ls_data.
    ls_data = VALUE #( invoicedquantity = 1000 unitcode = 'C62' ).
    CALL TRANSFORMATION ztransfo
        SOURCE abaproot = ls_data
        RESULT XML DATA(xml).
    
  • Simple Transformation ZTRANSFO:
    <?sap.transform simple?>
    <tt:transform xmlns:tt="http://www.sap.com/transformation-templates"
          xmlns:ddic="http://www.sap.com/abapxml/types/dictionary">
      <tt:root name="ABAPROOT"/>
      <tt:template>
      <ROOT xmlns:cbc="http://xxx" tt:ref="ABAPROOT">
        <cbc:InvoicedQuantity unitCodeListID="UNECRec20">
          <tt:attribute name="unitCode" value-ref="UNITCODE"/>
          <tt:value ref="INVOICEDQUANTITY"/>
        </cbc:InvoicedQuantity>
      </ROOT>
      </tt:template>
    </tt:transform>
    
  • Expected XML result:
    <?xml version="1.0" encoding="UTF-8"?>
    <ROOT xmlns:cbc="http://xxx">
      <cbc:InvoicedQuantity unitCodeListID="UNECRec20" unitCode="C62">
        1000
      </cbc:InvoicedQuantity>
    </ROOT>
    
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • I checked your solution, set it like this: ``` ``` When I'm trying to activate it, prints an error in `````` line. Error msg: "Content of element wasn't expected" (something like this) – TyRRRax Oct 26 '20 at 06:36
  • Hmm. You're right, I didn't test my solution. I've got the error `unknown root name 'INVOICEDQUANTITY' ` -> solved by adding the root (`tt:ref="ABAPROOT"`) and then `expected no element content` because `tt:value-ref` is not authorized if the node has some content, here an attribute -> solved by moving it after `` as ``. – Sandra Rossi Oct 26 '20 at 07:51
-2

Instead of:

unitCode="C62"

you can use:

unitCode="{UnitCode}"

or more precisely:

unitCode="{path/to/UnitCode}"

where path/to/UnitCode is the path to the UnitCode element from the context of the current node.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • For information, the OP did a mistake. It's about a SAP Simple Transformation (because of `tt:value-ref`), not XSLT. – Sandra Rossi Oct 23 '20 at 16:48