Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 8th, 2011, 04:59 AM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Default XSLT for converting column based xml to row based one

Hi,
I have an input xml file in the format given below:
Code:
<Column ID = "5">
<RefData>
<Data ID="1"> data for id 1</Data>
<Data ID="2"> data for id 2</Data>
<Data ID="3"> data for id 3</Data>
<Data ID="4"> data for id 4</Data>
</RefData>
</Column>

<Column ID="10">
<Measurements>

<Measurement>
<Label> 1 </Label>
<Data2>data2 for label 1 </Data2>
</Measurement>

<Measurement>
<Label> 2 </Label>
<Data2>data2 for label 2 </Data2>
</Measurement>

<Measurement>
<Label> 3 </Label>
<Data2>data2 for label 3 </Data2>
</Measurement>

<Measurement>
<Label> 4 </Label>
<Data2>data2 for label 4 </Data2>
</Measurement>

</Column>
Now, i need to translate this xml file to the following file:

Code:
<Measurementset>

<measurement>
<ID>1</ID>
<Data1>data for id 1</Data1>
<Data2>data2 for label 1</Data2>
</measurement>
<measurement>
<ID>2</ID>
<Data1>data for id 2</Data1>
<Data2>data2 for label 2</Data2>
</measurement>

<measurement>
<ID>3</ID>
<Data1>data for id 3</Data1>
<Data2>data2 for label 3</Data2>
</measurement>

<measurement>
<ID>1</ID>
<Data1>data for id 4</Data1>
<Data2>data2 for label 4</Data2>
</measurement>
</Measurementset>
I am not sure if I can perform the translation using xslt. Please let me know if this is possible using xslt.
 
Old March 8th, 2011, 05:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Assuming that for the n'th RefData/Data item you simply want the same value from the n'th Data2 element in the second column then you could use something like this:

Code:
  <xsl:template match="/">   
      <Measurementset>
		       <xsl:apply-templates select="//Column[@ID='5']"/>
	     </Measurementset>
   </xsl:template>
 
   <xsl:variable name="m" select="//Column[@ID='10']/Measurements/Measurement"/>
 
  <xsl:template match="RefData/Data">
      <measurement>
		       <xsl:variable name="pos" select="position()"/>
		       <ID>
            <xsl:value-of select="@ID"/>
         </ID>
		       <Data1>
            <xsl:value-of select="text()"/>
         </Data1>
		       <Data2>
            <xsl:value-of select="$m[$pos]/Data2"/>
         </Data2>
	     </measurement>
  </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
sayusekhar (March 8th, 2011)
 
Old March 8th, 2011, 08:06 AM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.
Actually the ID from Refdata/Data has to be matched with the Label from Measurements/Measurement.
 
Old March 8th, 2011, 08:12 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

In that case define a Key as so:

Code:
<xsl:key name="mKey" match="Measurement" use="normalize-space(Label)"/>
And then get the Data2 element like this:

Code:
<Data2><xsl:value-of select="key('mKey', @ID)/Data2"/></Data2>
You can get rid of the "pos" and "m" variables, as they are no longer needed.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
sayusekhar (March 8th, 2011)
 
Old March 8th, 2011, 08:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Assuming the input document is the following well-formed snippet
Code:
<Root>
<Column ID = "5">
<RefData>
<Data ID="1"> data for id 1</Data>
<Data ID="2"> data for id 2</Data>
<Data ID="3"> data for id 3</Data>
<Data ID="4"> data for id 4</Data>
</RefData>
</Column>


<Measurements>

<Measurement>
<Label> 1 </Label>
<Data2>data2 for label 1 </Data2>
</Measurement>

<Measurement>
<Label> 2 </Label>
<Data2>data2 for label 2 </Data2>
</Measurement>

<Measurement>
<Label> 3 </Label>
<Data2>data2 for label 3 </Data2>
</Measurement>

<Measurement>
<Label> 4 </Label>
<Data2>data2 for label 4 </Data2>
</Measurement>


</Measurements>

</Root>
then the stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:key name="k1" match="RefData/Data" use="@ID"/>
  
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="/">
    <xsl:apply-templates select="/Root/Measurements"/>
  </xsl:template>
  
  <xsl:template match="Measurement/Label">
    <xsl:apply-templates select="key('k1', normalize-space())"/>
  </xsl:template>
 
  <xsl:template match="RefData/Data">
    <ID><xsl:value-of select="@ID"/></ID>
    <Data1><xsl:value-of select="."/></Data1>
  </xsl:template>
  
</xsl:stylesheet>
creates the result:
Code:
<Measurements>
   <Measurement>
      <ID>1</ID>
      <Data1> data for id 1</Data1>
      <Data2>data2 for label 1 </Data2>
   </Measurement>
   <Measurement>
      <ID>2</ID>
      <Data1> data for id 2</Data1>
      <Data2>data2 for label 2 </Data2>
   </Measurement>
   <Measurement>
      <ID>3</ID>
      <Data1> data for id 3</Data1>
      <Data2>data2 for label 3 </Data2>
   </Measurement>
   <Measurement>
      <ID>4</ID>
      <Data1> data for id 4</Data1>
      <Data2>data2 for label 4 </Data2>
   </Measurement>
</Measurements>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
sayusekhar (March 8th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transform Attribute based xml to element based xml pallone XSLT 6 February 2nd, 2016 07:59 AM
Cell Coloring in VBA based on value in column FinalFive Excel VBA 3 March 24th, 2008 03:29 PM
extra column based on data in gridviewrow edwin.debrouwere ASP.NET 1.0 and 1.1 Basics 1 October 13th, 2006 09:18 AM
extra column based on data in gridviewrow edwin.debrouwere ASP.NET 1.0 and 1.1 Professional 0 October 13th, 2006 09:08 AM
Color the row in the datagrid based on date compar lily611 General .NET 2 March 30th, 2005 03:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.