![]() |
Languages »
XML »
XML/XSLT
Intermediate
XML Pivot To XMLBy Mike DiRenzoThis 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. |
C#, XML, XSLT, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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"
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>
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>
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!
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Mar 2007 Editor: Chris Maunder |
Copyright 2007 by Mike DiRenzo Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |