Enhancing Data Integration with XML Transformations
This section provides practical examples and guidance on leveraging the XML Transformation component within SQL Server Integration Services (SSIS) packages. The XML Transformation is a powerful tool for manipulating XML data, allowing you to format, filter, and restructure XML documents to meet specific business requirements.
Imagine you have an incoming XML document representing customer orders, and you need to transform it to a format suitable for a reporting service, focusing only on the order ID and customer name.
<Orders>
<Order id="1001">
<Customer name="Alice Smith">
<Item sku="A123" quantity="2"/>
<Item sku="B456" quantity="1"/>
</Customer>
<Date>2023-10-27</Date>
</Order>
<Order id="1002">
<Customer name="Bob Johnson">
<Item sku="C789" quantity="5"/>
</Customer>
<Date>2023-10-28</Date>
</Order>
</Orders>
Transform the source XML into a new structure containing only the order ID and customer name, like this:
<SimplifiedOrders>
<OrderSimplified orderId="1001" customerName="Alice Smith"/>
<OrderSimplified orderId="1002" customerName="Bob Johnson"/>
</SimplifiedOrders>
@id
attribute from the Order
element to a new attribute named orderId
in a target element OrderSimplified
.@name
attribute from the Customer
element to a new attribute named customerName
in the same OrderSimplified
element.You have XML data that needs to be presented in a human-readable HTML format for a web application. You can achieve this by applying an XSLT stylesheet.
<Products>
<Product id="P100">
<Name>Laptop</Name>
<Price currency="USD">1200.50</Price>
</Product>
<Product id="P101">
<Name>Keyboard</Name>
<Price currency="USD">75.00</Price>
</Product>
</Products>
product_list.xslt
)<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/Products">
<html>
<head>
<title>Product List</title>
<style>
body { font-family: sans-serif; }
table { border-collapse: collapse; width: 80%; margin: 20px auto; }
th, td { border: 1px solid #ccc; padding: 10px; text-align: left; }
th { background-color: #f2f2f2; }
</style>
</head>
<body>
<h1>Product Catalog</h1>
<table>
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Price</th>
</tr>
<xsl:apply-templates select="Product"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Product">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Price"/> <xsl:value-of select="Price/@currency"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
product_list.xslt
).