473,326 Members | 2,113 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,326 software developers and data experts.

XSL Sorting/Grouping question

I found the answer last time I was 'playing' with xsl, but I didn't need to use it then and like an idiot, I didn't save where I found it.
I've tried Google but it's not coming up with what I need.

From Access I'm exporting a file to xml in the following format:
Expand|Select|Wrap|Line Numbers
  1. <XMLTemp>
  2. <Who>JoeSmith</Who>
  3. <Contact>Billy Joe</Contact>
  4. <telephone>123457980</telephone>    
  5. <PostCode>XX1 2XX</PostCode>
  6. <When>01/01/2009</When>
  7.  
  8. <Who>JoeSmith</Who>
  9. <Contact>Brenda George</Contact>
  10. <telephone>3216549870</telephone>    
  11. <PostCode>XX5 3XX</PostCode>
  12. <When>01/01/2009</When>
  13.  
  14. <Who>KateLee</Who>
  15. <Contact>Iain Smith</Contact>
  16. <telephone>45687354189</telephone>    
  17. <PostCode>AB1 2XX</PostCode>
  18. <When>01/01/2009</When>
  19. </XMLTemp>
So that's 3 records, 2 for JoeSmith and one for KateLee. I'm going to be using another program to email them, so I need them in the following format:

Expand|Select|Wrap|Line Numbers
  1. <XMLTemp>
  2.  
  3. <Who>JoeSmith
  4. <Person>
  5. <Contact>Billy Joe</Contact>
  6. <telephone>123457980</telephone>    
  7. <PostCode>XX1 2XX</PostCode>
  8. <When>01/01/2009</When>
  9. </Person>
  10.  
  11. <Person>
  12. <Contact>Brenda George</Contact>
  13. <telephone>3216549870</telephone>    
  14. <PostCode>XX5 3XX</PostCode>
  15. <When>01/01/2009</When>
  16. </Person>
  17. </Who>
  18.  
  19.  
  20. <Who>KateLee
  21. <Person>
  22. <Contact>Iain Smith</Contact>
  23. <telephone>45687354189</telephone>    
  24. <PostCode>AB1 2XX</PostCode>
  25. <When>01/01/2009</When>
  26. </Person>
  27. </Who>
  28. </XMLTemp>
(If there's any case mistakes, I hand wrote this as an example, it'll all be good on the real version; also, line breaks are to make it easier to read)

I know about the for-each code:
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="//XMLtemp[*****]">
But I need help with filling in the [****]

I'm guessing it'll start [Who=, but I can't figure out how to make it equal the currently selected 'who'.

Thanks in advance
Mandi
Mar 24 '09 #1
7 2114
Dormilich
8,658 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="//Who">
will select JoeSmith's and KateLee's node, if that's what you mean...
Mar 24 '09 #2
Thanks Dormilich but that's not what I mean.
I want to only select each 'Who' once, and have all the contacts related to them listed below like this:
Expand|Select|Wrap|Line Numbers
  1. JoeSmith
  2. Person 1
  3. Person 2
  4. KateGeorge
  5. Person 3

I've got this much
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" indent="yes" encoding="UTF-8" />
  4. <xsl:template match="/">
  5. <Who>
  6.     <xsl:value-of select="//XMLTemp1/Who" />
  7.     <xsl:for-each select="//XMLTemp1">
  8.         <Person>
  9.             <Contact><xsl:value-of select="Contact" /></Contact>
  10.             <telephone><xsl:value-of select="telephone" /></telephone>    
  11.             <PostCode><xsl:value-of select="PostCode" /></PostCode>
  12.             <When><xsl:value-of select="FollowUpOn" /></When>
  13.         </Person>
  14.     </xsl:for-each>
  15. </Who>
  16. </xsl:template>
  17. </xsl:stylesheet>
Which gives me
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <Who>JoeSmith
  3.     <Person>
  4.         <Contact>Billy Joe</Contact>
  5.         <telephone>123457980</telephone>
  6.         <PostCode>XX1 2XX</PostCode>
  7.         <When>01/01/2009</When>
  8.     </Person>
  9. </Who>
I've used the for-each ones before, and it normally gives me all the information (3 persons in this case) but now it's just giving me one?
I'm missing something and I can't put my finger on it >_<
Mar 24 '09 #3
Dormilich
8,658 Expert Mod 8TB
@mandanarchi
you're looping over the root element, therefore you get only one entry.
Mar 24 '09 #4
I copied the line from a working code I have:
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="//XMLtemp[Suppliers='Advent']">
The problem is that I don't know what to replace [Suppliers='Advent'] with. That's just a static 'if suppliers = Advent then show it', I need a 'if Who = currently selected who then show it'.. Does that make sense?
Mar 24 '09 #5
Obvious. 'current()' was what I needed.
That plus a couple other minor edits and solved.
Always seems obvious once I've already asked.
Thanks folks
Mar 24 '09 #6
jkmyoung
2,057 Expert 2GB
I'm guessing you figured it out, but you should be looping on the Who nodes, not the base node.

<xsl:for-each select="//XMLTemp1/Who">
Mar 24 '09 #7
Here's my working solution. Ta for the help guys =)

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:output method="xml" indent="yes" encoding="UTF-8" />
  4. <xsl:template match="/">
  5. <ToEmail>
  6. <xsl:for-each select="//XMLTemp1[not(Who=preceding-sibling::XMLTemp1/Who)]/Who">
  7.     <Sales>
  8.     <who><xsl:value-of select="current()"/></who>
  9.         <xsl:for-each select="//XMLTemp1[Who=current()]">
  10.         <Person>
  11.             <Contact><xsl:value-of select="Contact" /></Contact>
  12.             <telephone><xsl:value-of select="telephone" /></telephone>    
  13.             <PostCode><xsl:value-of select="PostCode" /></PostCode>
  14.             <When><xsl:value-of select="FollowUpOn" /></When>
  15.         </Person>
  16.     </xsl:for-each>
  17. </Sales>
  18.   </xsl:for-each>
  19. </ToEmail>
  20. </xsl:template>
  21. </xsl:stylesheet>
Mar 26 '09 #8

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

Similar topics

1
by: Danny | last post by:
This relates to my question I submitted: How can I sort on two fields, one field will always have data in it like a SKU field. The other are different combinations of this SKU, only the parent...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
4
by: Marie | last post by:
My report has a text field named ItemNum. Most records have a value for ItemNum. I set Grouping And Sorting to sort ascending on the ItemNum field. The records where ItemNum is Null appear at the...
5
by: Mike | last post by:
Hello All, I have a report based upon a query. I have added a control to report footer that calcs the total cost of the inventory: =SUM(). When this total calculation is NOT on the report,...
8
by: nn0410 | last post by:
I have a report whose record source is a query. The query includes an ORDER BY clause that sorts on a particular set of columns. I would like to be able to run the same report with the same input...
6
by: Christoph | last post by:
I'm trying to come up with a stylesheet where, when the rows are displayed, duplicate game names are not shown on subsequent rows. It works but doesn't work properly. If I sort the data using...
3
by: Jimmy | last post by:
Is there a way to sort/group a report based on the second column of a combo box, i.e. the text associated with the primary key number?
3
by: Don | last post by:
I have a "Report" that is created from a "Form". It prints a list of items, you may consider it a shopping list. In any event I use to run this in alphabetical order but have since decided to run...
5
by: Gumbyu | last post by:
Hello all, I have been working on this issue for about a week and still cannot get anything to group or sort. At this point, I just want to be able to group a report by using a 'checkbox' on a form....
4
by: kstevens | last post by:
I have a report, and on the report is a min, avg, and max query each with (ok who really cares) 20 or so records. At the footer of the report i would like to have a total. I requery for each...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.