473,378 Members | 1,426 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,378 software developers and data experts.

XSLT grouping / xsl:key problem

1
Hi,

I have a problem with grouping. My source XML has <record> elements that have a @name and a @group attribute. It looks something like this:

Expand|Select|Wrap|Line Numbers
  1. <root>
  2.     <result>
  3.         <record name="test1" group="group1"><value>1A</value></record>
  4.         <record name="test2" group="group1"><value>1B</value></record>
  5.         <record name="test3" group="group1"><value>1C</value></record>
  6.         <record name="test1" group="group2"><value>2A</value></record>
  7.         <record name="test3" group="group2"><value>2F</value></record>
  8.         <record name="test4" group="group2"><value>2E</value></record>
  9.         <record name="test2" group="group3"><value>3A</value></record>
  10.         <record name="test3" group="group3"><value>3B</value></record>
  11.         <record name="test5" group="group3"><value>3E</value></record>
  12.     </result>
  13.     <result>
  14.         <record name="test1" group="group1"><value>1A</value></record>
  15.         <record name="test2" group="group1"><value>1B</value></record>
  16.         <record name="test3" group="group1"><value>1C</value></record>
  17.         <record name="test8" group="group4"><value>4D</value></record>
  18.         <record name="test9" group="group4"><value>4E</value></record>
  19.     </result>
  20. </root>
The goal is to have a html output, that shows the results as tables, one table per result which should look something like this:

---------------------------------------------
Result 1 | 1 | 2 | 3 |
---------------------------------------------
test1 | 1A | 2A | |
---------------------------------------------
test2 | 1B | | 3A |
---------------------------------------------
test3 | 1C | 2F | 3B |
---------------------------------------------
test4 | | 2E | |
---------------------------------------------
test5 | | | 3E |
---------------------------------------------

------------------------------------------------------
Result 2 | 1 | 2 | 3 | 4 |
------------------------------------------------------
test1 | 1A | | | |
------------------------------------------------------
test2 | 1B | | | |
------------------------------------------------------
test3 | 1C | | | |
------------------------------------------------------
test8 | | | | 4D |
------------------------------------------------------
test9 | | | | 4E |
------------------------------------------------------

For the numer of rows in the table I need to group the records by the @name attribute. I tried doing this with a <xsl:key>.

This is my XSLT stylesheet:

Expand|Select|Wrap|Line Numbers
  1. <xsl:key name="nameKey" match="record" use="@name"/>
  2. <xsl:template match="/">
  3.     <html>
  4.        <head>
  5.        </head>
  6.        <body>
  7.             <xsl:for-each select="root/result">
  8.                 <xsl:variable name="positionResult" select="position()"/>
  9.  
  10.                 <xsl:element name="table">
  11.                     <xsl:attribute name="border"><xsl:text>1</xsl:text></xsl:attribute>
  12.                     <xsl:element name="thead">
  13.                         <xsl:element name="tr">
  14.                             <xsl:element name="td">
  15.                                 <xsl:text>Result </xsl:text><xsl:value-of select="$positionResult"/>
  16.                             </xsl:element>
  17.                             <xsl:element name="td"><xsl:text>1</xsl:text></xsl:element>
  18.                             <xsl:element name="td"><xsl:text>2</xsl:text></xsl:element>
  19.                             <xsl:element name="td"><xsl:text>3</xsl:text></xsl:element>
  20.                             <xsl:element name="td"><xsl:text>4</xsl:text></xsl:element>
  21.                             <xsl:element name="td"><xsl:text>5</xsl:text></xsl:element>
  22.                         </xsl:element>
  23.                     </xsl:element>
  24.                     <xsl:element name="tbody">
  25.                         <xsl:for-each select="record[count(. | key('nameKey', @name)[1]) = 1]">
  26.                             <xsl:sort select="@name" order="ascending"/>
  27.                             <xsl:variable name="names" select="@name"/>
  28.  
  29.                             <xsl:element name="tr">
  30.                                 <xsl:element name="td"><xsl:value-of select="$names"/></xsl:element>
  31.                                 <xsl:element name="td"><xsl:value-of select="//result[$positionResult]/record[@name = $names and @group = 'group1']/value"/><xsl:text>*</xsl:text></xsl:element>
  32.                                 <xsl:element name="td"><xsl:value-of select="//result[$positionResult]/record[@name = $names and @group = 'group2']/value"/><xsl:text>*</xsl:text></xsl:element>
  33.                                 <xsl:element name="td"><xsl:value-of select="//result[$positionResult]/record[@name = $names and @group = 'group3']/value"/><xsl:text>*</xsl:text></xsl:element>
  34.                                 <xsl:element name="td"><xsl:value-of select="//result[$positionResult]/record[@name = $names and @group = 'group4']/value"/><xsl:text>*</xsl:text></xsl:element>
  35.                                 <xsl:element name="td"><xsl:value-of select="//result[$positionResult]/record[@name = $names and @group = 'group5']/value"/><xsl:text>*</xsl:text></xsl:element>
  36.                             </xsl:element>        
  37.  
  38.                         </xsl:for-each>
  39.                     </xsl:element>
  40.                 </xsl:element>
  41.             </xsl:for-each>
  42.         </body>
  43.     </html>
  44. </xsl:template>
Somehow however the resulting HTML does not show me the rows for test1, test2 and test3 for the second result table. Instead it looks like this:

------------------------------------------------------
Result 2 | 1 | 2 | 3 | 4 |
------------------------------------------------------
test8 | | | | 4D |
------------------------------------------------------
test9 | | | | 4E |
------------------------------------------------------

I think this is because the <xsl:key> for these has already been used in the first iteration ??! There is no way to predict the number of result elements in the source XML so I can't use a different <xsl:key> for each iteration...

Is there some other way to group the elements ?

Thank you very much !!

Leira
Jun 25 '09 #1
0 1740

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

Similar topics

0
by: Darren Gilroy | last post by:
I was wondering if there was a way to cache a built xsl:key? I have a custom resolver that returns a DOM when "document('Assets.xml')" is requested, but the stylesheet takes some amount of time...
7
by: Andy Fish | last post by:
Hi, I'm stuck with an XSL problem - can anyone give me any hints? I have some XML with nested formatting tags like this: <text> this is plain <bold> this is bold
6
by: chonkme | last post by:
Hi, i have a real simple xslt problem but i just cant figure out how to do it by looking at various examples on the net. i have a xml document and in it are some elements with a "result" tag name....
0
by: Mike | last post by:
I'm generating an XSLT document programatically in VB.Net. I'm then trying to apply that XSLT against a cXML document to generate my own internally developed XML document. I'm using RichTextBox...
6
by: Bloody Viking | last post by:
Namaste, Y'all. Given an element A containing elements B,C,D,E,F, I need to create a variable that gets the value of element C, conditionally followed by the value of element D, only if the...
4
by: Richard L Rosenheim | last post by:
Here's a sample XML file (created by saving a dataset): <?xml version "1.0" standalone="yes"?> <dsData xmlns="http://tempuri.org/DataSchema.xsd"> <Configuration> </Configuration> <Data>...
4
by: dwergkees | last post by:
Hi, Got a litte problem here. I'm trying to create a XSLT file that will do a transformation from WordML format (MS Word XML format, see...
0
by: akg250978 | last post by:
hello! i hope any1 in this forum can help me with my xsl problem, i am quite new to this programming and learning as i go along, this all part of my work, so for i was doing great till i came...
4
by: mark4asp | last post by:
I want to write a xslt template to create a xhtml 1.0 (transitional) file which will be sent in as email. Here is a typical xml data file: <BatchEmail> <Domain>www.myDomain.com</Domain>...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.