Click here to Skip to main content
Licence CPOL
First Posted 26 Mar 2007
Views 28,857
Downloads 136
Bookmarked 15 times

XML Pivot To XML

By | 27 Mar 2007 | Article
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 instrinsic functions of XSL:

<xsl:variable/> and <xsl:element/>.

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

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 said value as the element name. Confused? Think Pivot Table!

            <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:

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

The intent is to convert all Arg element values to elements with 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:

<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:

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

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

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

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

 
            <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)

About the Author

Mike DiRenzo

Web Developer

United States United States

Member

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 admire two well-renowned authors: Scott Ambler and Sal Mangano. Obviously there are many, many others but these two have been key to my successes over the years. I live and work in Charlotte, NC, USA and run a small S/W consulting practice called Business Automation Systems, Inc. I hold a Bachelors degree in Business with a concentration in Management Information Systems from the University of North Carolina at Charlotte.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice! PinmemberMember 4558663:21 11 Nov '09  
GeneralCall me slow, but... PinsitebuilderShog96:57 26 Mar '07  
GeneralRe: Call me slow, but... Pinmemberbug menot9:51 26 Mar '07  
GeneralRe: Call me slow, but... PinmemberMike DiRenzo13:35 26 Mar '07  
GeneralRe: Call me slow, but... PinmemberMike DiRenzo13:35 26 Mar '07  
GeneralRe: Call me slow, but... PinmemberMike DiRenzo2:30 27 Mar '07  
GeneralRe: Call me slow, but... PinsitebuilderShog95:55 27 Mar '07  
Thanks - makes sense now. Smile | :)
Shame about whoever gave you the 1-vote though.

 

----
It appears that everybody is under the impression that I approve of the documentation. You probably also blame Ken Burns for supporting slavery.
--Raymond Chen on MSDN

Generalnice! Pinmemberbrady gaster6:42 26 Mar '07  
GeneralRe: nice! PinmemberMike DiRenzo13:35 26 Mar '07  
GeneralRe: nice! PinmemberMike DiRenzo2:28 27 Mar '07  
GeneralRe: nice! Pinmemberbrady gaster2:36 27 Mar '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 27 Mar 2007
Article Copyright 2007 by Mike DiRenzo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid