473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Tricky XSLT Transformation

1
Hi all,

I am attempting to create an xslt transformation which I am having trouble with. I am fairly new to xslt and am having a hard time getting my head around it. The original xml file looks like this:

Expand|Select|Wrap|Line Numbers
  1. <DataSet ID="-5480">
  2.     <ObsGroup>
  3.         <ClsItem name="AGE">2099</ClsItem>
  4.         <ClsItem name="COMB_DATA">20</ClsItem>
  5.         <ClsItem name="SEX">2</ClsItem>
  6.         <ClsItem name="STATE">0</ClsItem>
  7.         <ClsItem name="MARITAL_STATUS">3</ClsItem>
  8.         <ParItem name="UNEMPLOYMENT_RATE">23.34</ParItem>
  9.         <ParItem name="LABFORCE_FULL_TIME">2292.7071594375</ParItem>
  10.         <TimeItem name="TIME_CODE">2447</TimeItem>
  11.     </ObsGroup>
  12.     <ObsGroup>
  13.         <ClsItem name="AGE">2099</ClsItem>
  14.         <ClsItem name="COMB_DATA">20</ClsItem>
  15.         <ClsItem name="SEX">2</ClsItem>
  16.         <ClsItem name="STATE">0</ClsItem>
  17.         <ClsItem name="MARITAL_STATUS">3</ClsItem>
  18.         <ParItem name="UNEMPLOYMENT_RATE">23.66</ParItem>
  19.         <ParItem name="LABFORCE_FULL_TIME">2300.72108466884</ParItem>
  20.         <TimeItem name="TIME_CODE">2451</TimeItem>
  21.     </ObsGroup>
  22.     <ObsGroup>
  23.         <ClsItem name="AGE">2099</ClsItem>
  24.         <ClsItem name="COMB_DATA">20</ClsItem>
  25.         <ClsItem name="SEX">2</ClsItem>
  26.         <ClsItem name="STATE">0</ClsItem>
  27.         <ClsItem name="MARITAL_STATUS">4</ClsItem>
  28.         <ParItem name="UNEMPLOYMENT_RATE">23.77</ParItem>
  29.         <ParItem name="LABFORCE_FULL_TIME">2353.4791599122</ParItem>
  30.         <TimeItem name="TIME_CODE">2447</TimeItem>
  31.     </ObsGroup>
  32.     <ObsGroup>
  33.         <ClsItem name="AGE">2099</ClsItem>
  34.         <ClsItem name="COMB_DATA">20</ClsItem>
  35.         <ClsItem name="SEX">2</ClsItem>
  36.         <ClsItem name="STATE">0</ClsItem>
  37.         <ClsItem name="MARITAL_STATUS">4</ClsItem>
  38.         <ParItem name="UNEMPLOYMENT_RATE">23.88</ParItem>
  39.         <ParItem name="LABFORCE_FULL_TIME">2335.71727933868</ParItem>
  40.         <TimeItem name="TIME_CODE">2451</TimeItem>
  41.     </ObsGroup>
  42. </DataSet>
And I want the transformation to group by the clsitem and one paritem per group, as 'measures'. This group will then have all the timeitem elements related to it. Example output:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <GenericData>
  3.     <DataSet>
  4.         <Series>
  5.             <SeriesKey>    
  6.                 <Value concept="MEASURES" value="UNEMPLOYMENT_RATE"/>
  7.                 <Value concept="AGE" value="2099"/>
  8.                 <Value concept="COMB_DATA" value="20"/>
  9.                 <Value concept="SEX" value="2"/>
  10.                 <Value concept="STATE" value="0"/>
  11.                 <Value concept="MARITAL_STATUS" value="3"/>
  12.             </SeriesKey>
  13.             <Obs>
  14.                 <Time>2447</Time>
  15.                 <ObsValue value="23.34"/>
  16.             </Obs>
  17.             <Obs>
  18.                 <Time>2451</Time>
  19.                 <ObsValue value="23.66"/>
  20.             </Obs>
  21.         </Series>
  22.         <Series>
  23.             <SeriesKey>
  24.                 <Value concept="MEASURES" value="LABFORCE_FULL_TIME"/>
  25.                 <Value concept="AGE" value="2099"/>
  26.                 <Value concept="COMB_DATA" value="20"/>
  27.                 <Value concept="SEX" value="2"/>
  28.                 <Value concept="STATE" value="0"/>
  29.                 <Value concept="MARITAL_STATUS" value="3"/>
  30.             </SeriesKey>
  31.             <Obs>
  32.                 <Time>2477</Time>
  33.                 <ObsValue value="2292.7071594375"/>
  34.             </Obs>
  35.             <Obs>
  36.                 <Time>2451</Time>
  37.                 <ObsValue value="2300.72108466884"/>
  38.             </Obs>
  39.         </Series>
  40.         <Series>
  41.             <SeriesKey>
  42.                 <Value concept="MEASURES" value="UNEMPLOYMENT_RATE"/>
  43.                 <Value concept="AGE" value="2099"/>
  44.                 <Value concept="COMB_DATA" value="20"/>
  45.                 <Value concept="SEX" value="2"/>
  46.                 <Value concept="STATE" value="0"/>
  47.                 <Value concept="MARITAL_STATUS" value="4"/>
  48.             </SeriesKey>
  49.             <Obs>
  50.                 <Time>2447</Time>
  51.                 <ObsValue value="23.77"/>
  52.             </Obs>
  53.             <Obs>
  54.                 <Time>2451</Time>
  55.                 <ObsValue value="23.88"/>
  56.             </Obs>
  57.         </Series>        
  58.         <Series>
  59.             <SeriesKey>
  60.                 <Value concept="MEASURES" value="LABFORCE_FULL_TIME"/>
  61.                 <Value concept="AGE" value="2099"/>
  62.                 <Value concept="COMB_DATA" value="20"/>
  63.                 <Value concept="SEX" value="2"/>
  64.                 <Value concept="STATE" value="0"/>
  65.                 <Value concept="MARITAL_STATUS" value="4"/>
  66.             </SeriesKey>
  67.             <Obs>
  68.                 <Time>2447</Time>
  69.                 <ObsValue value="2353.4791599122"/>
  70.             </Obs>
  71.             <Obs>
  72.                 <Time>2451</Time>
  73.                 <ObsValue value="2335.71727933868"/>
  74.             </Obs>
  75.         </Series>
  76.     </DataSet>
  77. </GenericData>
  78.  
My xslt is not grouping them correctly and I think I am on the wrong path. I have tried several grouping functions also, but they don't look right. My closest useful attempt:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
  3.     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  4.     <xsl:template match="/">
  5.     <GenericData >
  6.     <DataSet>
  7.     <xsl:for-each select="DataSet/ObsGroup/ParItem">
  8.         <Series>
  9.             <SeriesKey>
  10.                 <Value>
  11.                    <xsl:attribute name="concept">MEASURES</xsl:attribute>
  12.                    <xsl:attribute name="value"><xsl:value-of select="@name" /></xsl:attribute>               
  13.                 </Value>
  14.                 <xsl:for-each select="../ClsItem">
  15.                 <Value>
  16.                    <xsl:attribute name="concept"><xsl:value-of select="@name" /></xsl:attribute>
  17.                    <xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>               
  18.                 </Value>                
  19.                 </xsl:for-each>
  20.                 <xsl:for-each select="../TimeItem">
  21.                    <Obs>
  22.                       <Time><xsl:value-of select="." /></Time>
  23.                       <ObsValue>
  24.                          <xsl:attribute name="value"><xsl:value-of select="../ParItem" /></xsl:attribute>
  25.                       </ObsValue>
  26.                    </Obs>
  27.                 </xsl:for-each>
  28.             </SeriesKey>
  29.         </Series>
  30.     </xsl:for-each>
  31.     </DataSet>
  32.     </GenericData>
  33.     </xsl:template>
  34. </xsl:stylesheet>
Which doesn't group the time elements and also doesn't select the proper paritem value in the for-each loop.

I am not sure how I should be approaching this. Anyone have any thoughts?
Jan 15 '10 #1
1 2188
Dormilich
8,658 Expert Mod 8TB
I think, you need something like Munchian Grouping
Jan 15 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
4
by: Stephen | last post by:
I have the following that outputs an xml file to a div using ajax: <script type="text/javascript"> function ajaxXML(url,control_id){ if (document.getElementById) { var x =...
5
by: shauldar | last post by:
Is there a way (tool, hack...) to create an XSL:FO from an XSLT + XML files? My motivation is that we want to use a tool to design reports, and from that "design" generate both HTML (via XSLT)...
4
by: simon.a.hulbert | last post by:
Hi, I'm trying to view the following xslt transformation using firefox <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method = "xml" indent =...
2
by: Jonny B | last post by:
Hi all, I'm working on an a clientside xslt using jscript that passes a parameter to the xsl. I've got it working no problem in IE but cant get it to work in Mozilla. Can anyone help? This is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.