Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and XML →  Transformations for XML →  asXML - Canonical XML Representation →  asXML, Examples of Mappings 

asXML, Mapping of XML Fragments

This example demonstrates the mapping of XML fragments.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: xstr    TYPE xstring,
          xsdstr  TYPE xsdany,
          xmlstr1 TYPE xstring,
          xmlstr2 TYPE xstring.

    out = cl_demo_output=>new( ).

    xstr = prepare_fragment( ).

    xmlstr1 = serialize(
      source = xstr
      node   = 'ROOT'
      trafo  = 'ID'
      text =  'XSLT-Serialization of XML-Fragment from XSTRING' ).

    xsdstr = xstr.

    xmlstr1 = serialize(
      source = xsdstr
      node   = 'ROOT'
      trafo  = 'ID'
      text =  'XSLT-Serialization of XML-Fragment from XSDANY' &
              ' into node ROOT' ).

    xmlstr2 = serialize(
      source = xsdstr
      node   = 'X'
      trafo  = 'ID'
      text =  'XSLT-Serialization of XML-Fragment from XSDANY' &
              ' into node X' ).


    deserialize(
      EXPORTING
        source = xmlstr1
        node   = 'ROOT'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node ROOT' &
                 ' into XSTRING'
      IMPORTING result = xstr ).
    deserialize(
      EXPORTING
        source = xmlstr2
        node   = 'X'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node X' &
                 ' into XSTRING'
      IMPORTING result = xstr ).

    deserialize(
      EXPORTING
        source = xmlstr1
        node   = 'ROOT'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node ROOT' &
                 ' into XSDANY'
      IMPORTING result = xsdstr ).
    deserialize(
      EXPORTING
        source = xmlstr2
        node   = 'X'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node X' &
                 ' into XSDANY'
      IMPORTING result = xsdstr ).

    deserialize(
      EXPORTING
        source = xmlstr1
        node   = 'X'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node ROOT' &
                 ' into XSDANY using node X'
      IMPORTING result = xsdstr ).
    deserialize(
      EXPORTING
        source = xmlstr2
        node   = 'ROOT'
        trafo  = 'ID'
        text   = 'XSLT-Deserialization of XSLT-result with node X' &
                 ' into XSDANY using node ROOT'
      IMPORTING result = xsdstr ).

    out->display( ).

Description

This example demonstrates the serialization and deserialization of XML fragments and their representation in asXML using the identity transformation ID.

  • First, an XML fragment with the root element X is created in a variable xstr with the type xstring and the result displayed.
  • The fragment is serialized from the variable xstr of type xstring to an element ROOT in accordance with the rules for the mapping of elementary data types. The XML schema type xsd:base64Binary is used as a format.
  • On the other hand, a serialization of the fragment from the variable xsdstr of type XSDANY adds the attribute asx:root with the content "X" (that is, the name of the root element of the XML fragment) to the element ROOT and represents the remaining content of the fragment without modification.
  • A further serialization of the fragment from the variable xsdstr produces the element X. This has the same name as the root element of the XML fragment and so the attribute is not added.
  • Any attempt to deserialize the fragment content to an ABAP variable of type xstring fails for both serialization results, since the attribute specified for NODE when mapping elementary data types is not supported or because no valid asXML format exists for the target variable.
  • Deserializations to variables of type XSDANY are possible in both cases, however, and produce the original XML fragment again in the variable.
  • If a nonexistent element is addressed in deserializations, no deserialization is performed (as usual).

For information about serializations and deserializations of XML fragments using simple transformations, see the executable example ST, Mapping of XML Fragments.