Connecting Tech Pros Worldwide Help | Site Map

XSL Sorting/Grouping question

Member
 
Join Date: Sep 2008
Posts: 95
#1: Mar 24 '09
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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Mar 24 '09

re: XSL Sorting/Grouping question


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...
Member
 
Join Date: Sep 2008
Posts: 95
#3: Mar 24 '09

re: XSL Sorting/Grouping question


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 >_<
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#4: Mar 24 '09

re: XSL Sorting/Grouping question


Quote:

Originally Posted by mandanarchi View Post

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 >_<

you're looping over the root element, therefore you get only one entry.
Member
 
Join Date: Sep 2008
Posts: 95
#5: Mar 24 '09

re: XSL Sorting/Grouping question


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?
Member
 
Join Date: Sep 2008
Posts: 95
#6: Mar 24 '09

re: XSL Sorting/Grouping question


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
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#7: Mar 24 '09

re: XSL Sorting/Grouping question


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">
Member
 
Join Date: Sep 2008
Posts: 95
#8: Mar 26 '09

re: XSL Sorting/Grouping question


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>
Reply


Similar XML bytes