简体   繁体   中英

Simple Transformation deserialization: Match Element Error

I have to parse the following XML into an internal table, I get the short dump, after running the program. Please see the program and simple transformation in the following. An exception occurred in simple transformation that is explained in detail below.

The exception, which is assigned to class 'CX_ST_MATCH_ELEMENT', was not caught and therefore caused a runtime error.

The reason for the exception is:

XML matching error Expected was element-end: "Total" [ ] Read was element-start: "Weighing"

My XML:

CONCATENATE '<Automatic>'
'        <Weighing Tolerance="5" Deviation="1.60" >'
'          <Substance ExtId="3"   Speed="1" />'
'          <Substance ExtId="22"  Speed="5" />'
'        </Weighing>'
'        <Weighing Tolerance="5" Deviation="-16" >'
'          <Substance ExtId="22" Speed="5" />'
'        </Weighing>'
'        <Weighing  Tolerance="5" >'
'          <Substance ExtId="22" Speed="5" />'
'        </Weighing>'
'       <Total State="0" Result="Ok" />'
'      </Automatic>'
INTO DATA(xml).

Program:

* Substance *
TYPES: BEGIN OF ty_data,
        ExtId TYPE CHAR20,
        Speed TYPE CHAR20,
       END OF ty_data.
DATA: wa_ty_data   TYPE ty_data,
      lt_data TYPE TABLE OF ty_data.

  * Total *
TYPES: BEGIN OF ty_data3,
        State  TYPE CHAR20,
        Result TYPE CHAR20,
       END OF ty_data3.
DATA:  wa_ty_data3 TYPE ty_data3,
       lt_data3 TYPE TABLE OF ty_data3.

 * Weighing *
TYPES: BEGIN OF ty_data2,
        Tolerance  TYPE CHAR20,
        Deviation  TYPE CHAR20,
        t_data     TYPE TABLE OF ty_data WITH DEFAULT KEY,
        t_data3    TYPE TABLE OF ty_data3 WITH DEFAULT KEY,
       END OF ty_data2.
DATA: wa_ty_data2 TYPE ty_data2,
      lt_data2 TYPE TABLE OF ty_data2.

CALL TRANSFORMATION zpp_tt_exampletest_st
    SOURCE XML xml
    RESULT xml_tab = lt_data2.

ZPP_TT_EXAMPLETEST_ST transformation:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
 <tt:root name="XML_TAB"/>
 <tt:template>
   <Automatic>
     <tt:loop name="b" ref=".XML_TAB">
       <Weighing>
        <tt:cond check="not-initial($b.TOLERANCE)">
         <tt:attribute name="Tolerance" value-ref="$b.TOLERANCE"/>
        </tt:cond>
        <tt:cond check="not-initial($b.DEVIATION)">
         <tt:attribute name="Deviation" value-ref="$b.DEVIATION"/>
        </tt:cond>
         <tt:loop name="a" ref="$b.T_DATA">
          <Substance>
           <tt:cond check="not-initial($a.EXTID)">
             <tt:attribute name="ExtId" value-ref="$a.EXTID"/>
           </tt:cond>
           <tt:cond check="not-initial($a.SPEED)">
            <tt:attribute name="Speed" value-ref="$a.SPEED"/>
           </tt:cond>
         </Substance>
       </tt:loop>
       <Total>
        <tt:cond check="not-initial($b.STATE)">
          <tt:attribute name="State" value-ref="$b.STATE"/>
        </tt:cond>
        <tt:cond check="not-initial($b.RESULT)">
          <tt:attribute name="Result" value-ref="$b.RESULT"/>
        </tt:cond>
      </Total>
    </Weighing>
  </tt:loop>
 </Automatic>
</tt:template>
</tt:transform>

Many thanks in advance.

There is a mismatch between the XML and the ST. In your XML, <Total> is positioned after all <Weighing> elements, but in your Simple Transformation, <Total> is defined inside the <Weighing> element.

EDIT to cross the T's, after T.Ars request, considering that it's the ST to be corrected, move the </Weighing></tt:loop> - Of course, that's one possibility deducted from this one XML example, it could be wrong in case of other XMLs (like a random order of Weighing and Total elements), so theorically an ST could be provided only based on a XML schema (XSD, which defines the syntax of the XML), not on one XML example :

What you have:

      </tt:loop>
      <Total>
        <tt:cond check="not-initial($b.STATE)">
          <tt:attribute name="State" value-ref="$b.STATE"/>
        </tt:cond>
        <tt:cond check="not-initial($b.RESULT)">
          <tt:attribute name="Result" value-ref="$b.RESULT"/>
        </tt:cond>
      </Total>
    </Weighing>
  </tt:loop>
</Automatic>

What you should do:

    </Weighing>
  </tt:loop>
  <Total>
    <tt:cond check="not-initial($b.STATE)">
      <tt:attribute name="State" value-ref="$b.STATE"/>
    </tt:cond>
    <tt:cond check="not-initial($b.RESULT)">
      <tt:attribute name="Result" value-ref="$b.RESULT"/>
    </tt:cond>
  </Total>
</Automatic>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM