Click here to Skip to main content
15,881,755 members
Articles / Programming Languages / XSLT

XML Pivot To XML

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
27 Mar 2007CPOL1 min read 55.2K   263   15   11
This article describes how to convert an existing XML set of values that are contained in elements to valid elements of another XML, using an XSL transformation.

Introduction

This article describes how to convert an existing XML set of values that are contained in elements to valid elements of another XML using a XSL transformation.

Overview

Pivoting an arbitrary set of data such that its values become columns is often accomplished using Excel's Pivot Table feature or maybe some complicated T-SQL statement. Using a simple XSL technique, one can accomplish this using intrinsic functions of XSL: <xsl:variable/> and <xsl:element/>.

To be clear, let me qualify the source of xsl using the following common namespace declaration:

XML
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

The XML

This simple XML data will be used to convert a particular value contained in a particular element to a valid XML element with the said value as the element name. Confused? Think Pivot Table!

XML
<NewDataSet title="XMLPivotXML">
    <Table>
        <Arg>ArgValue123</Arg>
        <CtxtKeyID>CTXT123</CtxtKeyID>
        <CtxtName>ElementValue237</CtxtName>
    </Table>
    <Table>
        <Arg>ArgValue196</Arg>
        <CtxtKeyID>CTXT133</CtxtKeyID>
        <CtxtName>ElementValue238</CtxtName>
    </Table>
</NewDataSet>

The XSL

In the following example, I am using Arg as the desired value to convert to an element. Note the code snippet:

XML
<xsl:variable name='arg'><xsl:value-of select="Arg" /></xsl:variable>

The intent is to convert all Arg element values to elements with the said values as their respective names through transformation. In the following transformations, I replace Arg with other valid elements contained in the source XML. Here is the full XSL:

XML
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/"> 
        <args>
            <xsl:attribute name="ContextName">
                <xsl:value-of select="NewDataSet/@title"/>
            </xsl:attribute>
            <xsl:for-each select="//Table">
                <xsl:variable name='arg'><xsl:value-of select="Arg" />
                </xsl:variable>
                <xsl:element name='{$arg}'/>
            </xsl:for-each>
        </args>
    </xsl:template>
</xsl:stylesheet>        

The Transformation

Using Arg as the target element to convert, I get the following results:

XML
<args ContextName="XMLPivotXML">
    <ArgValue123 />
    <ArgValue196 />
</args>

Using CtxtKeyID as the target element to convert, I get the following results:

XML
<args ContextName="XMLPivotXML">
    <CTXT123 />
    <CTXT133 />
</args>

Using CtxtName as the target element to convert, I get the following results:

XML
<args ContextName="XMLPivotXML">
    <ElementValue237 />
    <ElementValue238 />
</args>

As you can see, the possibilities are wide open with what one can accomplish using this simple technique. I was thinking of using this as a template function that accepts a parameter. Good luck and happy transforming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I have lived and worked throughout the globe (Brussels, London, Paris, West Indies, USA) as an IT consultant. My favorite S/W development concentrations are XML/XSLT, C#, Agile Model - Driven Development. I live and work in Charlotte, NC, USA. I hold a Bachelors degree in Business with a concentration in Management Information Systems from the University of North Carolina at Charlotte.

Comments and Discussions

 
GeneralNice! Pin
RAND 45586611-Nov-09 3:21
RAND 45586611-Nov-09 3:21 
GeneralCall me slow, but... Pin
Shog926-Mar-07 6:57
sitebuilderShog926-Mar-07 6:57 
GeneralRe: Call me slow, but... Pin
Thanks for all the fish26-Mar-07 9:51
Thanks for all the fish26-Mar-07 9:51 
GeneralRe: Call me slow, but... Pin
Mike DiRenzo26-Mar-07 13:35
Mike DiRenzo26-Mar-07 13:35 
GeneralRe: Call me slow, but... Pin
Mike DiRenzo26-Mar-07 13:35
Mike DiRenzo26-Mar-07 13:35 
GeneralRe: Call me slow, but... Pin
Mike DiRenzo27-Mar-07 2:30
Mike DiRenzo27-Mar-07 2:30 
GeneralRe: Call me slow, but... Pin
Shog927-Mar-07 5:55
sitebuilderShog927-Mar-07 5:55 
Generalnice! Pin
brady gaster26-Mar-07 6:42
brady gaster26-Mar-07 6:42 
GeneralRe: nice! Pin
Mike DiRenzo26-Mar-07 13:35
Mike DiRenzo26-Mar-07 13:35 
GeneralRe: nice! Pin
Mike DiRenzo27-Mar-07 2:28
Mike DiRenzo27-Mar-07 2:28 
GeneralRe: nice! Pin
brady gaster27-Mar-07 2:36
brady gaster27-Mar-07 2:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.