473,788 Members | 3,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tricky XSLT Transformation

1 New Member
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 2207
Dormilich
8,658 Recognized Expert Moderator Expert
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
2712
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 extension responsible for the xslt functions. The problem i have is that the obtained transformation is not the waited one. I try to proccess the same XML file with XSL file with a program called XMLspy and i obtained the desire and waited...
6
2748
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 straight XML file http://www.discovertravelandtours.com/test/templates/test.xml?Location=Germany To another XML file http://www.discovertravelandtours.com/test/templates/test2.xml?Location=Germany
6
2933
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 this, and came across a problem that I haven't been able to solve elegantly. The problem is to find "linker" vertexes that a pair of verteces from a pre-defined set. For example, if the graph verteces represent cities and edges represent flights...
12
3237
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 (xml->html) doen't happen! I have XSLT files for this, they work, I mean when I put the output of the app as an XML file on some server, and make it use the XSLT files to transform into HTML, it works, I get a HTML page.
1
3110
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 Transformer convert that via XSLT to valid XSL-FO. So I define a SAXReader which fires the SAX Events for the Java Object. This works fine and the Transformation to PDF is ok. However, I have one object which contains an XHTML String and the tags
4
5251
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 = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); } if (x) {
5
4416
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) and PDF (via XSL:FO). The only tool we have seen is Altova's StyleVision, which is very unfriendly (and uses a proprietary representation, SPS, from which it generates the various XSLs). We have considered instead using InfoPath, which is WYSIWYG,...
4
2144
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 = "yes" /> <xsl:attribute-set name = "set1" > <xsl:attribute name = "a" >1</xsl:attribute> <xsl:attribute name = "b" >2</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name = "set2" >
2
3100
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 my IE code - <html> <head> <title>CEL Learning</title>
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.