Skip to content

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

asXML, Mapping of XML Fragments

The example demonstrates the mapping of XML fragments.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA: str    TYPE string,
          xstr   TYPE xstring,
          xsdstr TYPE xsdany,
          xmlstr TYPE xstring.

    str = `<?xml version="1.0" encoding="utf-8" ?>` &&
          `<X>`              &&
            `Text`           &&
            `<X1>Text1</X1>` &&
            `<X2>Text1</X2>` &&
          `</X>`.
    CALL TRANSFORMATION id
      SOURCE XML str
      RESULT XML xstr.
    cl_abap_browser=>show_xml(
      EXPORTING
        xml_xstring = xstr
        title       = 'XML-Fragment' ).

    CALL TRANSFORMATION id
      SOURCE node = xstr
      RESULT XML xmlstr.
    cl_abap_browser=>show_xml(
      EXPORTING
        xml_xstring = xmlstr
        title       = 'Serialization of XML-Fragment from XSTRING' ).

    xsdstr = xstr.
    CALL TRANSFORMATION id
      SOURCE node = xsdstr
      RESULT XML xmlstr.
    cl_abap_browser=>show_xml(
      EXPORTING
        xml_xstring = xmlstr
        title       = 'Serialization of XML-Fragment from XSDANY' ).

    TRY.
        CALL TRANSFORMATION id
          SOURCE XML xmlstr
          RESULT node = xstr.
      CATCH cx_xslt_format_error.
        MESSAGE 'Error during deserialization into normal type'
                TYPE 'I'.
    ENDTRY.

    CALL TRANSFORMATION id
      SOURCE XML xmlstr
      RESULT node = xsdstr.
    cl_abap_browser=>show_xml(
      EXPORTING
        xml_xstring = xsdstr
        title       = 'Deserialization into XSDANY' ).

Description

This example demonstrates how XML fragments can be represented in asXML.

  • Initially, a transformation from XML to XML generates an XML fragment with the root element X in a variable xstr of the type xstring and displays the result.
  • The fragment is serialized from the variable xstr of type xstring to an element NODE 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 NODE and represents the remaining content of the fragment without modification.
  • An attempt to deserializes the fragment content to an ABAP variable fails, since the attribute specified by NODE is not supported when mapping elementary data types.
  • Deserializations to variables of type XSDANY are possible, however, and generate the original XML fragment again in the variable.