Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 23rd, 2009, 08:36 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default Trying to sort within a sorted list.

I have the following xslt

<xsl:for-each select="$dcr">
<xsl:sort select="location"/>
<tr valign="top">
<td valign="top">
<xsl:variable name="unique-location" select="location[not(.=preceding::location)]"/>
<xsl:if test="active = 'Yes'">
<b><xsl:value-of select="$unique-location"/></b><br/>
<a name="{$unique-location}" href="$PAGE_LINK[information/about/Careers/careers-detail]dcr={@dcr}">
<xsl:value-of select="jobtitle"/></a>
</xsl:if>
</td>
</tr>
</xsl:for-each>



I am trying to achieve the following.....

LocationA
Jobtitle A
Jobtitle B

Location B
Jobtitle A
Jobtitle B

The user wants the jobtitle under each location to be sorted alphabetically.

I already have the location sorted alphabetically. I need help with the sorting of the jobtitles alphabetically within each location - thanks
 
Old February 23rd, 2009, 08:43 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you show us the XML input you want to process?
Also please state whether you want to use XSLT 2.0 or 1.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 23rd, 2009, 08:50 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks for your response.

My xml is as follows........

<careers dcr="/test/Careers/data/LondonTesting">
<location>London</location>

<jobtitle>LondonTesting</jobtitle>
</careers>

<careers dcr="/test/Careers/data/TokyoTesting">
<location>Tokyo</location>

<jobtitle>Head of Testing - Tokyo</jobtitle>
<careers>
 
Old February 23rd, 2009, 09:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 2.0 solution that first groups and sorts on location, then sorts on jobtitle:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output method="html" indent="yes"/>
  
  <xsl:template match="root">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <ul>
          <xsl:for-each-group select="careers" group-by="location">
            <xsl:sort select="location"/>
            <li>
              <xsl:value-of select="current-grouping-key()"/>
              <ul>
                <xsl:for-each select="current-group()/jobtitle">
                  <xsl:sort select="."/>
                  <li>
                    <xsl:value-of select="."/>
                  </li>
                </xsl:for-each>
              </ul>
            </li>
          </xsl:for-each-group>
        </ul>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
When run against this XML input:
Code:
<root>


<careers dcr="/test/Careers/data/TokyoTesting">
<location>Tokyo</location>

<jobtitle>Head of Testing - Tokyo</jobtitle>
</careers>

<careers dcr="/test/Careers/data/LondonTesting">
<location>London</location>

<jobtitle>foo</jobtitle>
</careers>

<careers dcr="/test/Careers/data/LondonTesting">
<location>London</location>

<jobtitle>bar</jobtitle>
</careers>

<careers dcr="/test/Careers/data/TokyoTesting">
<location>Tokyo</location>

<jobtitle>foo</jobtitle>
</careers>
<careers dcr="/test/Careers/data/LondonTesting">
<location>London</location>

<jobtitle>LondonTesting</jobtitle>
</careers>
</root>
it outputs

Code:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Example</title>
   </head>
   <body>
      <ul>
         <li>London
            <ul>
               <li>LondonTesting</li>
               <li>bar</li>
               <li>foo</li>
            </ul>
         </li>
         <li>Tokyo
            <ul>
               <li>Head of Testing - Tokyo</li>
               <li>foo</li>
            </ul>
         </li>
      </ul>
   </body>
</html>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 23rd, 2009, 10:01 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

I am assuming that your solution is based on xslt 2.0 b/c I'm getting an error on
Code:
current-grouping-key


DO you have a solution for xslt 1.0o that I could use within my existing
code -
Code:
<xsl:for-each select="$dcr">
             <xsl_sort select="location"/>
              <tr valign="top">
           <td valign="top">
                      <xsl:variable name="unique-location" select="location[not(.=preceding::location)]"/>
                      <xsl:if test="active  = 'Yes'">
                        <b><xsl:value-of select="$unique-location"/></b><br/>
                         <a name="{$unique-location}" href="$PAGE_LINK[information/about/Careers/careers-detail]dcr={@dcr}">
                          <xsl:value-of select="jobtitle"/></a>
                      </xsl:if>
                 </td>
         </tr>
            
             </xsl:for-each>
Which also uses preceding::location to get the unique locations?

Thanks
 
Old February 23rd, 2009, 10:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 1.0 stylesheet that uses Muenchian grouping to find distinct locations and then sorts each group based on the jobtitle. Result of the stylesheet is the same as for the XSLT 2.0 stylesheet

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="html" indent="yes"/>
  
  <xsl:key name="by-location" match="careers" use="location"/>
  
  <xsl:template match="root">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <ul>
          <xsl:for-each select="careers[generate-id() = generate-id(key('by-location', location)[1])]">
            <xsl:sort select="location"/>
            <li>
              <xsl:value-of select="location"/>
              <ul>
                <xsl:for-each select="key('by-location', location)/jobtitle">
                  <xsl:sort select="."/>
                  <li>
                    <xsl:value-of select="."/>
                  </li>
                </xsl:for-each>
              </ul>
            </li>
          </xsl:for-each>
        </ul>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old February 23rd, 2009, 10:41 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

You are a star Mate, that worked like a charm.

I 'll have to study closely to figure out what its doing. - Thanks again.
 
Old March 4th, 2009, 11:24 AM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

This is a follow up post - I have just discovered a problem

this is my xsl.....

Code:
<xsl:for-each select="$dcr[generate-id() = generate-id(key('by-location', location)[1])]">
             <xsl:sort select="location"/>
              <tr valign="top">
           <td valign="top">
                      <xsl:if test="active  = 'Yes'">
                        <b><a name="{location}"><xsl:value-of select="location"/></a></b><br/>
                        <xsl:for-each select="key('by-location',location)/jobtitle">
                          <xsl:sort select="."/> 
                          <a href="$PAGE_LINK[information/about/Careers/careers-detail]dcr={@dcr}"><xsl:value-of select="."/></a><br/>
                        </xsl:for-each>   
                      </xsl:if>
                </td>
         </tr>
             </xsl:for-each>
also my xml is below...


Code:
<careers  dcr="/xxxx/Careers/data/ij-NewYorkHeadTesting">
     <location>New York</location> 

    <jobtitle>New York Head Of Testing</jobtitle> 

    <departmentoverview>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050AAAAAAA060AAAAAAA070AAAAAAA080AAAAAAA090AAAAAAA100AAAAAAA110AAAAAAA120AAAAAAA130AAAAAAA140AAAAAAA150AAAAAAA160AAAAAAA170AAAAAAA180AAAAAAA190AAAAAAA200AAAAAAA210AAAAAAA220AAAAAAA230AAAAAAA240AAAAAAA250AAAAAAA260AAAAAAA270AAAAAAA280AAAAAAA290AAAAAAA300AAAAAAA310AAAAAAA320AAAAAAA330AAAAAAA340AAAAAAA350AAAAAAA360AAAAAAA370AAAAAAA380AAAAAAA390AAAAAAA400AAAAAAA410AAAAAAA420AAAAAAA430AAAAAAA440AAAAAAA450AAAAAAA460AAAAAAA470AAAAAAA480AAAAAAA490AAAAAAA500</departmentoverview> 

    <positionsummary>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050AAAAAAA060AAAAAAA070AAAAAAA080AAAAAAA090AAAAAAA100AAAAAAA110AAAAAAA120AAAAAAA130AAAAAAA140AAAAAAA150AAAAAAA160AAAAAAA170AAAAAAA180AAAAAAA190AAAAAAA200AAAAAAA210AAAAAAA220AAAAAAA230AAAAAAA240AAAAAAA250AAAAAAA260AAAAAAA270AAAAAAA280AAAAAAA290AAAAAAA300AAAAAAA310AAAAAAA320AAAAAAA330AAAAAAA340AAAAAAA350AAAAAAA360AAAAAAA370AAAAAAA380AAAAAAA390AAAAAAA400AAAAAAA410AAAAAAA420AAAAAAA430AAAAAAA440AAAAAAA450AAAAAAA460AAAAAAA470AAAAAAA480AAAAAAA490AAAAAAA500</positionsummary> 

  - <businesscompetencies>
   - <dutiesandresponsibililites>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </dutiesandresponsibililites>


  - <educationandexperience>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </educationandexperience>


  - <commercialawareness>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </commercialawareness>


  - <managementrequirements>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </managementrequirements>


   </businesscompetencies>


  - <personalcompetencies>
   - <personalimpact>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </personalimpact>


  - <communication>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </communication>


  - <teamwork>
     <bulletpt>AAAAAAA010AAAAAAA020AAAAAAA030AAAAAAA040AAAAAAA050</bulletpt>  

   </teamwork>


   </personalcompetencies>


    <active>Yes</active> 

   </careers>
Essentially my problem, is that now that I have the nested for each, which works for the sorting of the job titles within the location, the @dcr only picks up the 1st dcr.


What do I have to do/add to make

Code:
<a href="$PAGE_LINK[information/about/Careers/careers-detail]dcr={@dcr}"><xsl:value-of select="jobtitle"/></a><br/>
work within the 2nd <xsl:for each />. It works within the first <xsl:for-each/>, but that is not what I need.

Last edited by rabs; March 4th, 2009 at 11:52 AM.. Reason: addition
 
Old March 4th, 2009, 12:49 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Inside the second for-each the context node is a 'jobtitle' element which does not seem to have any 'dcr' attribute. You might want parent::careers/@dcr instead of @dcr.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
rabs (March 4th, 2009)
 
Old March 4th, 2009, 01:07 PM
Authorized User
 
Join Date: Feb 2009
Posts: 43
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks again Martin - that worked. So does Parent just go back up one node?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sort by a value that's a comma seperated list medic XSLT 0 June 7th, 2006 09:52 AM
How to list files in the folder? -Sorted- Mantis PHP How-To 2 April 29th, 2005 06:17 AM
array list sort linfakngiau BOOK: Beginning Java 2 2 October 19th, 2003 12:50 PM
Search in a sorted list yajleejnus Classic ASP Basics 0 June 11th, 2003 04:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.