Dynamic XML Generation with SQL Data and XSLT Transformation - n8n Workflow

Learn how to build a complex n8n workflow to pull data from MySQL, structure it, convert it to XML, and format it using a dynamically served XSLT template for modern web delivery.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Backend Developers needing automatic data feed generation.
System Integrators requiring specific XML outputs for legacy systems or partners.
Users looking for advanced examples of data transformation within an n8n workflow.
Anyone interested in mastering the n8n node for database interaction and XML handling.

Overview

This advanced n8n workflow addresses the challenge of serving structured, dynamically generated XML data that requires complex formatting via an external XSLT stylesheet—a common requirement for integrating data feeds or accommodating browser security policies (CORS).

The implementation uses two separate n8n trigger endpoints. The primary endpoint fetches database information (using the MySQL n8n node), transforms the results into a single XML document, and includes an xml-stylesheet instruction pointing back to a secondary webhook path within the same n8n instance.

This secondary n8n trigger is crucial: it fetches the raw XSLT file from a remote source (like a GitHub Gist) and serves it with the correct content type (text/xsl), ensuring browsers can securely retrieve and apply the transformation to the primary XML data. This comprehensive n8n template demonstrates sophisticated data routing and content serving.

How it Works

This n8n workflow operates in two concurrent paths:

Path 1: Generating and Serving the XML Data


  1. The primary Webhook n8n trigger fires.

  2. The Show 16 random products n8n node connects to MySQL and fetches a limited dataset.

  3. The Define file structure Set n8n node restructures the flat database results into key-value pairs (Product.Code, Product.Name) appropriate for the desired XML hierarchy. This is a vital step in preparing data for the next n8n node.

  4. The Concatenate Items n8n node aggregates all individual data items (rows) into a single object under the root key Products.

  5. The Convert to XML n8n node transforms the aggregated JSON object into a raw XML string.

  6. The Create HTML n8n node (acting as a text formatter) wraps the XML string with the standard XML declaration and, critically, inserts the xml-stylesheet link that points to the secondary XSLT n8n trigger using the {{ $env.WEBHOOK_URL }} variable.

  7. The Move Binary Data n8n node converts this final XML text into binary data with the MIME type text/xml.

  8. The Respond to Webhook n8n node sends this binary XML data as the final response.

Path 2: Serving the Required XSLT Template


  1. When the browser receives the XML from Path 1, it requests the XSLT file from the secondary Request xsl template n8n trigger path.

  2. The Get XSLT HTTP Request n8n node fetches the raw XSLT file from a public URL (GitHub Gist).

  3. The Respond to Webhook1 n8n node serves the raw XSLT file back, ensuring the Content-Type header is correctly set to text/xsl for proper browser interpretation. This architecture demonstrates the flexibility of using multiple n8n trigger mechanisms in one solution.

Installation Guide

To deploy this powerful n8n workflow template:


  1. Import: Copy the provided JSON and import it into your n8n instance using the 'New' menu -> 'Import from JSON'.

  2. MySQL Credentials: The Show 16 random products n8n node requires a configured MySQL credential (named db4free MySQL in the example). You must replace this with your active database credentials connected to a table containing 'products'.

  3. Webhook Setup: Ensure your n8n instance is accessible externally. The workflow relies on the $env.WEBHOOK_URL environment variable being correctly configured so that the primary XML output can correctly reference the secondary XSLT n8n trigger.

  4. Activation: Ensure the n8n workflow is set to 'Active'.

  5. Testing: Execute the main Webhook n8n trigger URL in a browser to see the formatted XML output (or use an API client to inspect the raw XML and binary data response headers).

Node Details

Webhook (n8n trigger): Primary entry point for the XML generation process. It waits for the response from the Respond to Webhook n8n node.
Show 16 random products (MySQL n8n node): Executes SELECT from products ORDER BY RAND() LIMIT 16;. Essential for extracting the source data.
Define file structure (Set n8n node): Maps specific SQL column names (like productCode) to structured XML element names (like Product.Code), ensuring the resulting XML is correctly structured. This n8n node uses 'Keep Only Set' to prune extraneous data.
Concatenate Items (Item Lists n8n node): Aggregates the list of product rows into a single JSON object under the key Products, which the subsequent XML n8n node uses as the root structure.
Convert to XML (XML n8n node): Performs the JSON-to-XML transformation. Key configuration is Headless: true.
Create HTML (HTML n8n node): Used here to embed the XML declaration and the dynamic XSLT link within the data stream. It references the secondary XSLT n8n trigger URL using environment variables.
Move Binary Data (Move Binary Data n8n node): Converts the final text output into binary data (text/xml) required for the final webhook response.
Request xsl template (n8n trigger): The secondary n8n trigger specifically designed to serve the XSLT file.
Get XSLT (HTTP Request n8n node): Fetches the external XSLT template from GitHub, configured to treat the response as a file (binary).


  • Respond to Webhook (n8n node): Used twice (once per branch) to send back the appropriate binary data (XML or XSLT) with the correct content type headers, demonstrating robust control over the n8n workflow output.

Related n8n Workflows

Free

Nodes: 0 Nodes
Updated: December 26 2025
View all
Created by
n8n Team
n8n Team

Meet the official n8n team. We specialize in building workflows that transform intricate tasks into seamless operations.

Featured*