XML in SAP Integration
XML stands for eXtensible Markup Language. It is a text-based format for storing and transporting structured data. XML uses tags — similar to HTML — to label data and describe its meaning. Unlike HTML, which has fixed tag names like <p> and <h1>, XML lets you define your own tag names that match your business data.
XML is the dominant data format in SAP integration. IDocs, SOAP web services, and many older APIs all use XML. Even when you work with JSON or CSV data, CPI often converts it to XML internally for processing.
Reading XML: A Beginner Example
Here is what a simple purchase order looks like in XML:
<PurchaseOrder>
<OrderNumber>PO-12345</OrderNumber>
<Supplier>ABC Supplies Ltd</Supplier>
<OrderDate>2024-05-01</OrderDate>
<LineItems>
<Item>
<Material>Steel Rod 10mm</Material>
<Quantity>500</Quantity>
<Unit>KG</Unit>
<Price>2.50</Price>
</Item>
<Item>
<Material>Bolt M12</Material>
<Quantity>1000</Quantity>
<Unit>EA</Unit>
<Price>0.15</Price>
</Item>
</LineItems>
</PurchaseOrder>
Every piece of data sits between an opening tag (<OrderNumber>) and a closing tag (</OrderNumber>). Tags can be nested inside other tags, creating a tree structure. The entire document has one root element — in this case, <PurchaseOrder>.
XML Structure Rules
XML has strict rules. A document that breaks these rules is called not well-formed and CPI will reject it:
- Every opening tag must have a matching closing tag
- Tags must be properly nested — you cannot close an outer tag before closing its inner tags
- Tag names are case-sensitive — <Order> and <order> are different tags
- There must be exactly one root element that wraps everything else
- Special characters like &, <, and > inside data must use escape sequences (&, <, >)
XML Attributes
Tags can carry additional information as attributes inside the opening tag:
<Item sequence="1" unit="KG"> <Material>Steel Rod 10mm</Material> <Quantity>500</Quantity> </Item>
Attributes appear inside the opening tag as name="value" pairs. In SAP XML messages, attributes often carry technical metadata like document type, message version, or namespace identifiers.
XML Namespaces in SAP
When two XML vocabularies use the same tag name, namespaces prevent conflict. SAP XML messages almost always include namespace declarations. They look like this:
<ORDERS05 xmlns="urn:sap-com:document:sap:idoc:messages"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
...
</ORDERS05>
The xmlns attribute declares a namespace — a unique identifier (usually a URL) that defines the meaning of the tags. When you write XPath expressions or mappings in CPI, you often need to declare namespaces so the tool knows which tag vocabulary you are targeting.
XPath: Navigating XML
XPath is a query language for finding data inside XML documents. In CPI, you use XPath in routing conditions, value mappings, and Groovy scripts to extract specific values from a message.
XML Document:
<Order>
<Header>
<OrderID>ORD-999</OrderID>
<Status>Approved</Status>
</Header>
<Items>
<Item><SKU>A001</SKU><Qty>10</Qty></Item>
<Item><SKU>B002</SKU><Qty>5</Qty></Item>
</Items>
</Order>
XPath expressions:
/Order/Header/OrderID → returns "ORD-999"
/Order/Header/Status → returns "Approved"
/Order/Items/Item[1]/SKU → returns "A001" (first item)
/Order/Items/Item/Qty → returns all Qty values
count(/Order/Items/Item) → returns 2
XML Schema (XSD)
An XML Schema Definition (XSD) describes the allowed structure of an XML document. It defines which tags are allowed, in what order, whether they are mandatory or optional, and what data type each tag must contain.
In SAP integration, XSD files serve as contracts between systems. When you build a message mapping in CPI, you import the XSD for the source and target. CPI uses the schemas to present the correct structure in the mapping editor and validate messages at runtime.
XSD defines the rule: XML message either passes or fails <xs:element name="Qty" <Qty>500</Qty> ✓ Valid (number) type="xs:integer"/> <Qty>five</Qty> ✗ Invalid (text)
XSLT: Transforming XML
XSLT (Extensible Stylesheet Language Transformations) is a language for converting one XML structure into a different XML structure. CPI has a built-in XSLT step where you provide an XSLT stylesheet and CPI applies it to transform the incoming XML.
A simple XSLT example: converting a source XML field name "CustomerName" to a target field name "Name":
Source XML: After XSLT:
<Customer> <Buyer>
<CustomerName> <Name>
ACME Corp ACME Corp
</CustomerName> </Name>
</Customer> </Buyer>
XSLT is powerful but has a steep learning curve. For most integration projects, CPI's graphical message mapping is easier to use and covers the same transformations without writing XSLT manually. Advanced transformations covered in a later topic in this course explain when XSLT becomes the better choice.
XML in SAP IDocs
SAP IDocs (Intermediate Documents) use XML as their transport format when sent through CPI. The IDoc adapter in CPI converts between the internal SAP IDoc format and an XML representation that the iFlow can process. Every IDoc segment becomes an XML element, making the data accessible for mapping and transformation.
Understanding XML is therefore a prerequisite for working with IDocs, SOAP services, and most legacy SAP interfaces. The next two topics in this course cover JSON and IDocs specifically, building directly on the XML concepts introduced here.
