Working with the Extension XML File

The extension.xml file is the most important ingredient of your Data Hub extension. This file defines the data model and relationships of each of the three phases of the Data Hub workflow (load, compose, and publish). It describes your data to Data Hub.

The extension.xml file uses an easy-to-understand XML dialect that allows you to define a data model for your project. This data model describes the internal structure of raw items, canonical items, and target items within Data Hub. It also allows you to specify the necessary transformations the data must undergo on its journey from raw to canonical. Then to target items suitable for publishing to your target systems.

It is considered a best practice to maintain distinct extension.xml files for raw, canonical, and target items. This separation of data models for the different item types makes your data models easier to manage. Data Hub loads the extension files together during start-up and initialization. An extension.xml file is the minimum requirement for a custom Data Hub extension. You can easily create custom extension.xml files by modifying existing examples, or creating new files using your favorite XML editor or IDE.

The following sections briefly outline the XML dialect for each of the item types described by your extension.xml files, with examples. The examples are taken directly from the Hello World tutorial you completed earlier. In the tutorials later in this chapter, you then modify some of the attributes of these files to customize your data transformations.

Raw Item XML

A raw item represents the structure of the unprocessed data entering the Data Hub. Here is a simple example of the raw item extension.xml used for the Hello World tutorial.

  • <rawItems> contains a list of <item> definitions. Each <item> defines the structure of a raw item type.
  • Each <item> contains a <type>, a <description>, and a list of <attributes>.
  • The <attribute> for a raw item contains only a <name>.

You may think of the <type> as the name of a table in the database, with each <attribute> representing a column in that table. Your raw item data model should match as closely as possible your source data model, in both structure and naming. You apply transformations later, when composing raw items into canonical items.

<extension xmlns="http://www.hybris.com/schema/" name="hello-raw-datahub-extension">
    <rawItems>
        <item>
            <type>RawProduct</type>
            <description>Raw representation of a product</description>
            <attributes>
                <attribute>
                    <name>id</name>
                </attribute>
                <attribute>
                    <name>productName</name>
                </attribute>
                <attribute>
                    <name>productDescription</name>
                </attribute>
            </attributes>
        </item>  
	</rawItems>

Also included in our raw-item.xml file are the transformations for canonical items. The transformations define how raw item attributes are mapped to canonical item attributes. It is good practice to keep the transformations in your raw-item.xml because they are closely tied to raw item structure. If you must change source systems, you can easily replace both raw item structure and raw-canonical transformations by manipulating only one file. In this way you preserve the canonical data model, which is defined in your canonical-item.xml file.

Canonical item transformations have the following structure.

  • <canonicalItems> contains a list of <item> definitions. Each <item> defines the structure of a canonical item type.
  • Like the raw item XML, each canonical <item> contains a <type>, a <description>, and a list of <attributes>.
  • The <attribute> for a canonical item contains a <name>, and a list of <transformations> .
  • The <transformation> is where you define the mapping from raw items to canonical items. In the following example, <rawSource> is the source of the raw item. In this case, one of the rawItem <type>s defined in your raw item XML. The <expression> is the <name> of the rawItem <attribute> that should populate the canonical item <attribute> identified by its canonical item <name>.
    <canonicalItems>
        <item>
            <type>CanonicalProduct</type>
            <attributes>
                <attribute>
                    <name>identifier</name>
                    <transformations>
                        <transformation>
                            <rawSource>RawProduct</rawSource>
                            <expression>id</expression>
                        </transformation>
                    </transformations>
                </attribute>
                <attribute>
                    <name>name</name>
                    <transformations>
                        <transformation>
                            <rawSource>RawProduct</rawSource>
                            <expression>productName</expression>
                        </transformation>
                    </transformations>
                </attribute>
                <attribute>
                    <name>description</name>
                    <transformations>
                        <transformation>
                            <rawSource>RawProduct</rawSource>
                            <expression>productDescription</expression>
                        </transformation>
                    </transformations>
                </attribute>
            </attributes>
        </item>
    </canonicalItems>

Note that so far we have only defined the transformations. The actual canonical data model is defined in your canonical-item.xml.

Canonical Item XML

Here is a simple example of the canonical item extension.xml.

  • <canonicalItems> contains a list of <item> definitions, as described previously.
  • The <attribute> for a canonical item contains a <name>, and a <model> .
  • The <model> is where you specify certain attributes of the canonical item, such as the data <type>, and whether it constitutes a <primaryKey>.
<extension xmlns="http://www.hybris.com/schema/" name="hello-canonical-datahub-extension.xml">
    <canonicalItems>
        <item>
            <type>CanonicalProduct</type>
            <attributes>
                <attribute>
                    <name>identifier</name>
                    <model>
                        <localizable>false</localizable>
                        <collection>false</collection>
                        <type>String</type>
                        <primaryKey>true</primaryKey>
                    </model>
                </attribute>
                <attribute>
                    <name>name</name>
                    <model>
                        <localizable>true</localizable>
                        <collection>false</collection>
                        <type>String</type>
                        <primaryKey>false</primaryKey>
                    </model>
                </attribute>
                <attribute>
                    <name>description</name>
                    <model>
                        <localizable>true</localizable>
                        <collection>false</collection>
                        <type>String</type>
                        <primaryKey>false</primaryKey>
                    </model>
                </attribute>
            </attributes>
        </item>
    </canonicalItems>

The canonical data model is independent of both source and target systems. You therefore should not need to modify it, even when changing the source or target.

Target Item XML

The target item XML file differs only slightly in structure to the raw and canonical extension XML files. Data Hub can publish to more than one target, and each of target can have a unique schema. You can therefore have multiple target data models. For the purposes of this chapter, there is only one target system defined in our file.

The following are specific to target items:

  • The <transformationExpression> allows you to transform your data in some way. It can be done using simple mapping, similar to the way you map raw to canonical items. In that case, you simply provide the canonical item attribute name from which the item is to be sourced. Alternatively, you can use the Spring Expression Language (SpEL), and the custom SAP Commerce Cloud resolve() function for more complex transformations.
  • The <exportCode> provides the name of the target item attribute into which the canonical item data is to be placed. Here, once again, you can simply provide a name, but exportCode can also include an ImpEx expression.
<extension xmlns="http://www.hybris.com/schema/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.hybris.com/schema/ datahub-metadata-schema-1.0.0.xsd" name="hello-target-datahub-extension">

	<targetSystems>
        <targetSystem>
            <name>HybrisFileAdapter</name>
            <type>HybrisTest</type>
            <exportURL>${datahub.extension.exportURL}</exportURL>
            <userName>${datahub.extension.userName}</userName>
            <password>${datahub.extension.password}</password>

            <exportCodes/>

<!-- Target items and attributes -->
            <targetItems>
                <item>
                    <type></type>
                    <updatable>true</updatable>
                    <canonicalItemSource>CanonicalProduct</canonicalItemSource>
                    <status/>
                    <!-- defaults to ACTIVE -->
                    <attributes>
                        <attribute>
                            <name>identifier</name>
                            <localizable>false</localizable>
                            <collection>false</collection>
                            <transformationExpression>primaryKey</transformationExpression>
                            <exportCode>uid[unique=true]</exportCode>
                            <mandatoryInHeader>true</mandatoryInHeader>
                        </attribute>
                        <attribute>
                            <name>name</name>
                            <localizable>false</localizable>
                            <collection>false</collection>
                            <transformationExpression>name</transformationExpression>
                            <exportCode>name</exportCode>
                            <mandatoryInHeader>true</mandatoryInHeader>
                        </attribute>
                        <attribute>
                            <name>description</name>
                            <localizable>false</localizable>
                            <collection>false</collection>
                            <transformationExpression>description</transformationExpression>
                            <exportCode>description</exportCode>
                            <mandatoryInHeader>true</mandatoryInHeader>
                        </attribute>
                    </attributes>
                </item>
            </targetItems>
        </targetSystem>
    </targetSystems>
</extension>
In the tutorials to follow, you edit the target attributes of your Hello World tutorial XML files with various transformation expressions. These tutorials show how easy it is to transform simple data with Data Hub.