Systems Integration

Mapping Languages

Prof. Dr. Christian Drumm

10.11.2026

Overview

Mapping Languages

Transforming data between structures and formats

  • Query languages retrieve data — mapping languages transform it
  • Mapping languages convert data from one format or structure into another
  • Essential in data integration scenarios where systems use different formats
  • Three languages are covered in this lecture:
    • SQL — transformation features beyond plain querying
    • XSLT — the standard language for transforming XML
    • jq — a widely used filter language for transforming JSON
  • Mapping languages operate on the syntactic level of interoperability

Learning Objectives

After this lecture, you should be able to:

  • Describe the transformation concepts shared by all three languages: reshaping, filtering, and aggregating data
  • Distinguish the three transformation paradigms: set-based SQL, template-based XSLT, and filter pipelines in jq
  • Apply SQL’s set-based transformations in the database, including native XML and JSON generation
  • Explain why database implementations deviate from the SQL standard
  • Write XSLT stylesheets based on the template processing model, including XSLT 3.0’s built-in XML ⇄ JSON conversion
  • Compose jq filter pipelines to reshape, aggregate, and export JSON data
  • Select the appropriate mapping language for a given integration scenario

Querying vs. Mapping

Two complementary capabilities

Aspect Query languages Mapping languages
Goal Retrieve and filter data Transform and restructure data
Input Database, XML document, JSON Documents or tables in one format
Output The requested data Data in a new format or structure


The languages of this lecture

Language Query language role Mapping language role
SQL SELECT statements Joins, CTEs, XMLELEMENT, JSON_OBJECT
XSLT XPath path expressions Templates, fn:xml-to-json
jq Filters, e.g. select(.price > 100) Object and array construction, map, @csv


In practice, both concepts overlap: a query can already transform, and a mapping frequently queries its input.

Data Transformation with SQL

SQL as a Mapping Language

More than querying

  • SQL was introduced in the previous lecture as a query language
  • SQL also provides extensive functionality for transforming data:
    • reshaping tables and combining data
    • summarizing and analyzing data
    • generating XML and JSON output inside the database
  • This makes SQL a powerful tool for ETL and mapping scenarios

Structural Transformation

Reshaping data with joins, subqueries, and CTEs

  • SQL provides several features to transform and reorganize data structures:
    • Joins — combine data from multiple tables
    • Subqueries — nest queries for flexible filtering
    • Common Table Expressions (CTEs) — structure complex queries into manageable parts
  • These features allow you to reshape tables, combine datasets, and build complex mappings within a single statement

Joins

Combining data from multiple tables

  • Joins merge tables based on related keys
  • Inner Join — rows with a match in both tables
  • Left / Right Join — all rows from one side, matched rows from the other; non-matches return NULL
  • Full Outer Join — rows with a match in either table or both
SELECT p.name, p.price, c.category_name
FROM Products AS p
INNER JOIN Categories AS c
ON p.category_id = c.category_id;

Subqueries

Nesting queries for flexibility

  • Subqueries nest one query inside another
  • Useful when conditions depend on values from a secondary query:
Type Returns Used in
Scalar subquery a single value SELECT list or WHERE clause
Table subquery a result set FROM clause
SELECT name, price
FROM Products
WHERE price > (SELECT AVG(price) FROM Products);

Retrieves all products more expensive than the average product — the subquery computes the threshold.

Common Table Expressions (CTEs)

Structuring complex queries

  • CTEs break complex queries into smaller, reusable parts
  • Defined with the WITH clause; can be referenced multiple times
  • Particularly useful for recursion and hierarchical data
WITH InventoryValueSummary AS (
  SELECT category_id,
         SUM(price * stock) AS total_inventory_value
  FROM Products
  GROUP BY category_id
)

SELECT c.category_name, ivs.total_inventory_value
FROM InventoryValueSummary ivs
INNER JOIN Categories c ON ivs.category_id = c.category_id;

The CTE computes inventory values per category; the main query adds readable category names.

Aggregate Functions

Summarizing data

  • SQL’s aggregate functions summarize data:
Function Purpose
COUNT() counts rows or non-null values
SUM() sums numeric values
AVG() calculates the average of a column
MAX() / MIN() highest and lowest values
SELECT c.category_name,
       COUNT(p.id) AS number_of_products,
       AVG(p.price) AS average_price,
       SUM(p.price * p.stock) AS total_inventory_value
FROM Products p
JOIN Categories c ON p.category_id = c.category_id
GROUP BY c.category_name;

Window Functions

Advanced analysis

  • Window functions calculate across a set of rows related to the current row
  • Useful for running totals, ranking, and analytical queries
SELECT name, price,
       RANK() OVER (ORDER BY price DESC) AS rank
FROM Products;

Result:

name price rank
Laptop 1500 1
Smartphone 1000 2
Headphones 200 3

The function operates over a window of rows; each row keeps its own context.

SQL:2003 — Generating XML

Relational data to XML

  • Since SQL:2003, the standard supports an XML datatype and XML functions:
Function Purpose
XMLELEMENT creates an XML element with a name and content
XMLAGG aggregates XML elements into one document
XMLFOREST creates a forest of elements from columns
XMLQUERY() extracts data from XML via XPath-like expressions
SELECT XMLELEMENT(NAME "products",
         XMLAGG(
           XMLELEMENT(NAME "product",
             XMLFOREST(id AS "id", name AS "name", price AS "price"))))
FROM Products;
<products>
  <product><id>1</id><name>Mountain Bike</name><price>1200</price></product>
  <product><id>2</id><name>Helmet</name><price>50</price></product>
</products>

SQL:2016 — Generating JSON

Relational data to JSON

  • Since SQL:2016, the standard converts relational data into JSON; since SQL:2023 it also defines a JSON datatype
Function Purpose
JSON_OBJECT creates a JSON object from key-value pairs
JSON_ARRAYAGG aggregates values into a JSON array
JSON_VALUE() extracts scalar values from JSON columns
OPENJSON() parses JSON data into relational format
SELECT JSON_ARRAYAGG(
         JSON_OBJECT('id' VALUE id,
                     'name' VALUE name,
                     'price' VALUE price))
FROM Products;
[
  { "id": 1, "name": "Mountain Bike", "price": 1200 },
  { "id": 2, "name": "Helmet", "price": 50 }
]

Database-specific Implementations

When systems deviate from the standard

  • Not all systems implement the latest SQL standards
  • Many databases provide proprietary functions tailored to their architecture
  • Example DuckDB — an in-process analytical database:
-- DuckDB-specific JSON functions
SELECT json_group_array(
         json_object('id', id,
                     'name', name,
                     'price', price))
FROM Products;
  • json_group_array and json_object replace JSON_ARRAYAGG and JSON_OBJECT
  • Same mapping, different syntax — similar variations exist for XML functions

In integration projects, always check the actual implementation of your database system.

PIVOT in DuckDB

A powerful, non-standard transformation

  • PIVOT turns rows into columns — a high-level transformation not part of the SQL standard
  • DuckDB requires pivoted data to be prepared in a CTE
WITH InventoryValueSummary AS (
    SELECT c.category_name,
           p.price * p.stock AS inventory_value
    FROM Products p
    JOIN Categories c ON p.category_id = c.category_id
)
PIVOT InventoryValueSummary
ON category_name
USING SUM(inventory_value) AS total_inventory_value;

Result:

Outdoor Electronics Accessories
16500.00 75000.00 6250.00

XSLT

XSLT — Introduction

Extensible Stylesheet Language Transformations

  • A powerful language specifically designed for transforming XML documents
  • In data integration, XSLT transforms data from one XML schema to another
  • Also used to generate HTML, JSON, or plain text from XML
  • Transformation rules are defined in templates, selected via XPath expressions
  • W3C standard — current version XSLT 3.0

XSLT relies on XPath for selecting and manipulating parts of the XML document.

XSLT — Processing Model

Templates define the transformation

  • A stylesheet consists of templates — rules for transforming specific elements
  • Core instructions:
    • <xsl:template match="..."> — defines how matching elements are transformed
    • <xsl:apply-templates> — processes elements based on matching templates
    • <xsl:value-of select="..."/> — retrieves the text value of elements and attributes
  • XPath expressions drive the matching and selection

XSLT — Running Example

  • The same source document is used for all subsequent examples
<Products>
  <Product>
    <Name>Mountain Bike</Name>
    <Price>1200</Price>
  </Product>
  <Product>
    <Name>Helmet</Name>
    <Price>50</Price>
  </Product>
</Products>

XSLT — Template-Based Example

Transforming one XML schema to another

Stylesheet

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <TransformedData>
      <xsl:apply-templates
        select="Products/Product" />
    </TransformedData>
  </xsl:template>

  <xsl:template match="Product">
    <Item>
      <Name><xsl:value-of select="Name" /></Name>
      <Price><xsl:value-of select="Price" /></Price>
    </Item>
  </xsl:template>
</xsl:stylesheet>

Output

<TransformedData>
  <Item>
    <Name>Mountain Bike</Name>
    <Price>1200</Price>
  </Item>
  <Item>
    <Name>Helmet</Name>
    <Price>50</Price>
  </Item>
</TransformedData>
  • The root template (/) creates <TransformedData> and applies templates to all Product elements
  • Each Product becomes an <Item> with Name and Price extracted via <xsl:value-of>

XSLT — Conditional Logic

xsl:if and xsl:choose

  • Conditional structures enable selective transformation based on data content
  • Essential when parts of a document need different treatment
<xsl:template match="Product">
  <Item>
    <Name><xsl:value-of select="Name" /></Name>
    <xsl:if test="Price > 100">
      <Price><xsl:value-of select="Price" /></Price>
    </xsl:if>
  </Item>
</xsl:template>

Result:

<Item><Name>Mountain Bike</Name><Price>1200</Price></Item>
<Item><Name>Helmet</Name></Item>

The Price element is only included when the price exceeds 100.

XSLT — Iteration and Sorting

xsl:for-each and xsl:sort

  • xsl:for-each loops over elements to transform multiple parts of the document
  • xsl:sort sorts the XML data before it is transformed
<xsl:template match="/">
  <TransformedData>
    <xsl:for-each select="Products/Product">
      <xsl:sort select="Price" order="ascending" />
      <Item>
        <Name><xsl:value-of select="Name" /></Name>
        <Price><xsl:value-of select="Price" /></Price>
      </Item>
    </xsl:for-each>
  </TransformedData>
</xsl:template>

Products are sorted by Price before being transformed into Item elements.

XSLT — XML to JSON (Manually)

Constructing text output

  • XSLT is a general-purpose transformation language — it can also emit JSON as text
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="/">
    { "products": [
      <xsl:for-each select="Products/Product">
        { "name": "<xsl:value-of select='Name'/>",
          "price": <xsl:value-of select='Price'/> }<xsl:if
          test="position() != last()">,</xsl:if>
      </xsl:for-each>
    ] }
  </xsl:template>
</xsl:stylesheet>
  • JSON is built manually as plain text
  • <xsl:if> handles the commas between array elements

XSLT 3.0 — fn:xml-to-json

Built-in XML to JSON conversion

  • Since XSLT 3.0, the built-in function fn:xml-to-json() converts XML to JSON directly:
    • $input-node — the XML node or subtree to convert
    • $options — optional handling of elements, attributes, pretty-printing
  • The result is guaranteed to be well-formed JSON
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="/">
    <xsl:value-of select="fn:xml-to-json(.)" />
  </xsl:template>
</xsl:stylesheet>

The XML tree is automatically mapped to the equivalent JSON format — no manual formatting, no comma handling.

XSLT 3.0 — fn:json-to-xml

Built-in JSON to XML conversion

  • The reverse function fn:json-to-xml() converts JSON into XML
  • Useful when systems deliver JSON but the pipeline processes XML

Input JSON

{
  "Products": {
    "Product": [
      { "Name": "Mountain Bike", "Price": 1200 },
      { "Name": "Helmet", "Price": 50 }
    ]
  }
}

Resulting XML (excerpt)

<map:map xmlns:map="http://www.w3.org/
                   2005/xpath-functions">
  <map:string key="Products">
    <map:map>
      <map:string key="Product">
        <map:array>
          <map:map>
            <map:string key="Name">
              Mountain Bike</map:string>
            <map:number key="Price">1200
              </map:number>
          </map:map>
          ...
        </map:array>
      </map:string>
    </map:map>
  </map:string>
</map:map>

The JSON is represented as XML in the map: namespace; it can then be processed with any XSLT transformation.

XSLT — Advanced XPath Constructs

Filtering on calculated conditions

  • XPath predicates allow complex selection logic
  • Task: include only items whose total price (Price * Quantity) exceeds 1000
<xsl:template match="Order">
  <Order id="{@id}">
    <Customer><xsl:value-of select="Customer" /></Customer>
    <xsl:apply-templates
      select="Items/Item[Price * Quantity &gt; 1000]" />
  </Order>
</xsl:template>

<xsl:template match="Item">
  <Item>
    <Name><xsl:value-of select="Name" /></Name>
    <TotalPrice><xsl:value-of select="Price * Quantity" /></TotalPrice>
  </Item>
</xsl:template>

The predicate Items/Item[Price * Quantity > 1000] filters items by a calculated condition; the total price is computed and output as <TotalPrice>.

XSLT — Exercise

Writing XSLT Stylesheets

In this exercise, you will write XSLT stylesheets to transform a product catalog XML document.

  • Exercise 1 — XML-to-XML: transform the catalog into a new structure (Product/ProductName/Price → Item/Name/Cost)
  • Exercise 2 — XML-to-JSON: transform the same catalog into JSON with a catalog array
  • Check your results with an XSLT processor
  • Use the input documents from the lecture notes as a basis

Work in pairs and compare your stylesheets.

XSLT — Key Takeaways

  • XSLT is a versatile tool for transforming XML in integration scenarios
  • Template-based transformation allows selective processing of elements
  • Conditional logic and looping control how the output is structured
  • XSLT handles both XML-to-XML and XML-to-JSON conversions
    • XSLT 3.0 provides built-in fn:xml-to-json() and fn:json-to-xml()
  • Advanced XPath expressions enable complex filtering, e.g. on calculated conditions

jq

jq — Transformation Features

Reshaping and converting JSON

  • jq was introduced as a query language for JSON — every jq program is a filter
  • Filters can also transform JSON:
    • Object {...} and array [...] construction reshape documents
  • Built-ins: map, select, group_by, sort_by, add, del
  • Operators: + (add fields), |= / += (update), del() (remove)
  • Output formatters: @csv, @tsv, and -r raw output

jq is the most widely used tool for transforming JSON in integration pipelines.

jq — Running Example

The JSON document used throughout this section

{
  "products": [
    {
      "id": 1,
      "name": "Mountain Bike",
      "category": "Outdoor",
      "price": 1200,
      "stock": 10
    },
    {
      "id": 2,
      "name": "Helmet",
      "category": "Accessories",
      "price": 50,
      "stock": 100
    },
    {
      "id": 3,
      "name": "Gloves",
      "category": "Accessories",
      "price": 25,
      "stock": 50
    }
  ]
}

jq — Constructing New JSON

Object and array construction

Filter

[.products[] |
  {name: .name, cost: .price}]

Output

[
  { "name": "Mountain Bike", "cost": 1200 },
  { "name": "Helmet", "cost": 50 },
  { "name": "Gloves", "cost": 25 }
]
  • .products[] iterates over all products
  • The object construction renames keys (price → cost) and drops all other fields
  • [...] collects all results into a single JSON array

jq — Transforming and Enriching

Built-ins and operators

Built-in / Operator Purpose Example
map(f) apply f to every element .products \| map(.price) → [1200, 50, 25]
select(f) keep elements where f is true select(.price > 100)
+ add or merge fields . + {value: (.price * .stock)}
del(path) remove fields or elements del(.id)
add / length aggregate values map(.price) \| add → 1275
[.products[] | select(.price > 100)
              | . + {inventory_value: (.price * .stock)}]

The filter keeps products above a price of 100 and enriches them with a calculated inventory_value field.

jq — Grouping and Aggregating

The jq equivalent of SQL GROUP BY

.products
| group_by(.category)
| map({category: .[0].category,
       number_of_products: length,
       total_inventory_value: (map(.price * .stock) | add)})

Output:

[
  {
    "category": "Accessories",
    "number_of_products": 2,
    "total_inventory_value": 6250
  },
  {
    "category": "Outdoor",
    "number_of_products": 1,
    "total_inventory_value": 12000
  }
]
  • group_by(.category) produces one sub-array per category
  • map(...) transforms each group into a summary object
  • map(.price * .stock) | add aggregates the inventory value per group

jq — Output Beyond JSON

Exporting to CSV, TSV, and raw text

  • @csv / @tsv format an array as a CSV / TSV row
  • -r prints strings raw, without quotes
jq -r '.products[] | [.name, .price] | @csv' products.json

Output:

"Mountain Bike",1200
"Helmet",50
"Gloves",25

Useful to hand JSON data to systems that do not consume JSON — e.g. spreadsheets, relational ETL tools, or shell scripts.

jq — Exercise

Transforming and Exporting JSON

In this exercise, you will write jq filters to transform a product catalog JSON document.

  • Exercise 3 — JSON-to-JSON: extract all products with a price above 100 into a new structure (catalog array with name and price)
  • Exercise 4 — JSON-to-CSV: export all products as CSV, sorted by price descending
  • Check your results with jq play or locally: jq '<filter>' products.json
  • Use the input document from the lecture notes as a basis

Recall: sort_by() sorts arrays and @csv formats rows.

jq — Key Takeaways

  • jq is a filter language that queries and transforms JSON
  • Object and array construction reshape documents; +, |=, and del() modify fields
  • Built-ins such as map, select, group_by, and sort_by process collections
  • @csv and @tsv export data for non-JSON consumers
  • Widely used in integration pipelines to adapt JSON messages between systems

Summary

Summary

  • Mapping languages transform data between formats and structures — essential for systems integration
  • SQL goes beyond querying:
    • Structural transformation with joins, subqueries, and CTEs
    • Summarization with aggregate and window functions
    • Native XML (SQL:2003) and JSON (SQL:2016) generation
    • Implementations deviate from the standard, e.g. proprietary functions in DuckDB
  • XSLT transforms XML via templates and XPath:
    • Conditional logic, iteration, and sorting
    • XML-to-XML and XML-to-JSON, with built-in functions in XSLT 3.0
  • jq transforms JSON with filters:
    • Object and array construction reshape documents; map, select, group_by, and sort_by process collections
    • @csv and @tsv export JSON data for non-JSON consumers

SQL vs. XSLT vs. jq

Three languages, three data models

Aspect SQL XSLT jq
Data model Relational tables XML node tree JSON values
Transformation paradigm Declarative, set-based operations Rule-based templates driven by XPath Functional filter pipelines
Typical input Relational tables XML documents JSON values
Typical output Tables, XML, JSON XML, JSON, HTML, plain text JSON, CSV, TSV, plain text
Core constructs JOIN, subqueries, CTEs, GROUP BY, XMLELEMENT xsl:template, xsl:if, xsl:for-each, fn:xml-to-json Object/array construction, map, select, @csv
Standardization ISO/IEC 9075 (incl. SQL/XML, JSON functions) W3C standard, version 3.0 Not standardized, de facto standard tool
Integration use ETL and mapping inside the database Message mapping between XML systems Adapting JSON in pipelines

The languages are complementary: SQL transforms data inside the database, XSLT maps XML-centric systems, and jq adapts the JSON exchanged by modern APIs — often used in combination.

Case Study: BikeERP and its Partners

One product catalog, three systems, three data formats


BikeERP

  • Manages the product catalog in a relational database
  • Tables: Products, Categories

BikeShop24

  • The web shop, sells online
  • Reads the catalog from a REST API
  • Expects data in JSON

PartsHub

  • A supplier portal, legacy system
  • Demands weekly price lists as XML

Required data flows

  • ERP → shop: publish the full catalog as JSON
  • ERP → supplier: deliver the price list as XML

Which mapping language do you use for each of the three data flows, and why?

References