XSL Transformations

XSL Transformations

This is a really good resource for xsl transformations. One of the useful things to me is the section about using xsl:element and xsl:attribute when transforming from xml to xml.


I had an interesting case where I needed to transform an “extensible” database model into a more usable xml structure.

This is the original output of a SQL Server stored proc using For Xml Auto:

<transaction transactionid=”1″>
<vendor vendordescription=”DU”>
<status statusdescription=”Created”>
<transactionattribute attributeid=”1″ attributekey=”TestAttribute1″
attributevalue=”TestValue1″ attributedatatype=”String”>
<transactionattribute attributeid=”2″ attributekey=”TestAttribute2″
attributevalue=”TestValue2″ attributedatatype=”String”>

I needed the output to be in a format somewhat like this:


So, to accomplish this I created this xsl transformation script:

<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”&gt;
<xsl:output method=”xml” indent=”yes” omit-xml-declaration=”no” />
<xsl:template match=”/”>
<xsl:element name=”TransactionID”>
<xsl:value-of select=”//Transaction/@TransactionID” />
<xsl:element name=”VendorName”>
<xsl:value-of select=”//Vendor/@VendorDescription” />
<xsl:element name=”Status”>
<xsl:value-of select=”//Status/@StatusDescription” />
<xsl:for-each select=”//TransactionAttribute”>
<xsl:element name=”{@AttributeKey}”>
<xsl:value-of select=”@AttributeValue” />


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s