Mapping Languages
10.11.2026
Transforming data between structures and formats
After this lecture, you should be able to:
| 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 |
| 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.
More than querying
Reshaping data with joins, subqueries, and CTEs
Combining data from multiple tables
NULLDiagram: A. Martinson, “You Should Use This to Visualize SQL Joins Instead of Venn Diagrams”_
Nesting queries for flexibility
| Type | Returns | Used in |
|---|---|---|
| Scalar subquery | a single value | SELECT list or WHERE clause |
| Table subquery | a result set | FROM clause |
Retrieves all products more expensive than the average product — the subquery computes the threshold.
Structuring complex queries
WITH clause; can be referenced multiple timesThe CTE computes inventory values per category; the main query adds readable category names.
Summarizing 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 |
Advanced analysis
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.
Relational data to XML
| 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 |
Relational data to JSON
| 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 |
When systems deviate from the standard
json_group_array and json_object replace JSON_ARRAYAGG and JSON_OBJECTIn integration projects, always check the actual implementation of your database system.
A powerful, non-standard transformation
PIVOT turns rows into columns — a high-level transformation not part of the SQL standardResult:
| Outdoor | Electronics | Accessories |
|---|---|---|
| 16500.00 | 75000.00 | 6250.00 |
Extensible Stylesheet Language Transformations
XSLT relies on XPath for selecting and manipulating parts of the XML document.
Templates define the transformation
<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 attributesTransforming one XML schema to another
<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>/) creates <TransformedData> and applies templates to all Product elementsProduct becomes an <Item> with Name and Price extracted via <xsl:value-of>xsl:if and xsl:choose
Result:
The
Priceelement is only included when the price exceeds 100.
xsl:for-each and xsl:sort
xsl:for-each loops over elements to transform multiple parts of the documentxsl:sort sorts the XML data before it is transformedProducts are sorted by
Pricebefore being transformed intoItemelements.
Constructing text output
<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><xsl:if> handles the commas between array elementsfn:xml-to-jsonBuilt-in XML to JSON conversion
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-printingThe XML tree is automatically mapped to the equivalent JSON format — no manual formatting, no comma handling.
fn:json-to-xmlBuilt-in JSON to XML conversion
fn:json-to-xml() converts JSON into XML<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.
Filtering on calculated conditions
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 > 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>.
Writing XSLT Stylesheets
In this exercise, you will write XSLT stylesheets to transform a product catalog XML document.
Product/ProductName/Price → Item/Name/Cost)catalog arrayWork in pairs and compare your stylesheets.
fn:xml-to-json() and fn:json-to-xml()Reshaping and converting JSON
{...} and array [...] construction reshape documentsmap, select, group_by, sort_by, add, del+ (add fields), |= / += (update), del() (remove)@csv, @tsv, and -r raw outputjq is the most widely used tool for transforming JSON in integration pipelines.
The JSON document used throughout this section
Object and array construction
.products[] iterates over all productsprice → cost) and drops all other fields[...] collects all results into a single JSON arrayBuilt-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 |
The filter keeps products above a price of 100 and enriches them with a calculated
inventory_valuefield.
The jq equivalent of SQL GROUP BY
Output:
group_by(.category) produces one sub-array per categorymap(...) transforms each group into a summary objectmap(.price * .stock) | add aggregates the inventory value per groupExporting to CSV, TSV, and raw text
@csv / @tsv format an array as a CSV / TSV row-r prints strings raw, without quotesOutput:
Useful to hand JSON data to systems that do not consume JSON — e.g. spreadsheets, relational ETL tools, or shell scripts.
Transforming and Exporting JSON
In this exercise, you will write jq filters to transform a product catalog JSON document.
catalog array with name and price)jq '<filter>' products.jsonRecall:
sort_by()sorts arrays and@csvformats rows.
+, |=, and del() modify fieldsmap, select, group_by, and sort_by process collections@csv and @tsv export data for non-JSON consumersmap, select, group_by, and sort_by process collections@csv and @tsv export JSON data for non-JSON consumersThree 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.
One product catalog, three systems, three data formats
Products, CategoriesWhich mapping language do you use for each of the three data flows, and why?
fn:xml-to-json, fn:json-to-xmlSystems Integration | Mapping Languages | Prof. C. Drumm