xsl-list
[Top] [All Lists]

Re: [xsl] sort on one key - duplicates

2017-03-07 04:01:19

On 7 Mar 2017, at 09:44, Dr. Patrik Stellmann 
patrik(_dot_)stellmann(_at_)gdv-dl(_dot_)de 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi Raimund,

You could just replace the "l_name" by "concat(l_name, ' ', f_name)" within 
the xsl:key, key() and xsl:sort.


A better solution is to use a multi-part sort key:

<xsl:for-each ...
   <xsl:sort select="l_name"/>
   <xsl:sort select="f_name"/>

The results will only be different if either

(a) your default collation ignores spaces, or

(b) the names you are sorting on include spaces

Michael Kay
Saxonica


------------------------------------------------------------------
Systemarchitektur & IT-Projekte
Tel: +49 40 33449-1142
Fax: +49 40 33449-1400
E-Mail: mailto:Patrik(_dot_)Stellmann(_at_)gdv-dl(_dot_)de

-----Ursprüngliche Nachricht-----
Von: Raimund Kammering raimund(_dot_)kammering(_at_)desy(_dot_)de 
[mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com]
Gesendet: Dienstag, 7. März 2017 10:32
An: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Betreff: [xsl] sort on one key - duplicates

Hi Folks,

I have an XSL which pulls out all last names and creates a sorted list of 
these. Works fine, but now I noticed that for duplicated last names only the 
first person is created in the output! So I need to also take the first name 
into account! But I’m lacking the idea to accomplish this! The XML, XSL looks 
like this:

<list>
  <subject name=“A">
     <person>
        <l_name>Doe</l_name>
        <f_name>John</f_name>
        <expert/>
        <mail>john(_dot_)doe(_at_)somewhere(_dot_)com</mail>
     </person>
     <person>
        <l_name>Doe</l_name>
        <f_name>Johanna</f_name>
        <expert/>
        <mail>johanna(_dot_)doe(_at_)somewhere(_dot_)com</mail>
     </person>
  </subject>

  <subject name=“B”>
    <person>
        <l_name>Mueller</l_name>
        <f_name>Michael</f_name>
        <expert/>
        <mail>michael(_dot_)mueller(_at_)somewhere(_dot_)com</mail>
     </person>
     <person>
        <l_name>Mueller</l_name>
        <f_name>Joe</f_name>
        <expert/>
        <mail>joe(_dot_)mueller(_at_)somewhere(_dot_)com</mail>
     </person>
  </subject>
</list>

with the following XSL

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
version="2.0">

 <xsl:output indent="yes" method="xml"/>

 <!-- Needed to avoid duplicates in "All" subject see:             -->
 <!-- http://www.jenitennison.com/xslt/grouping/muenchian.htm -->
 <xsl:key name="l_names" match="subject/person" use="l_name"/>

 <xsl:template match="/list">
   <xsl:choose>
     <xsl:when test="count(./*[@name='All']) = 0">
       <xsl:element name="{name()}">
         <xsl:element name="subject">
           <xsl:attribute name="name">All</xsl:attribute>
           <xsl:for-each select="subject/person[count(. | key('l_names', 
l_name)[1]) = 1]">
             <xsl:sort select="l_name"/>
             <xsl:element name="person">
               <xsl:copy-of select="l_name"/>
               <xsl:copy-of select="f_name"/>
               <xsl:element name="expert"/>
               <xsl:copy-of select="mail"/>
             </xsl:element>
           </xsl:for-each>
         </xsl:element>

         <xsl:for-each select="subject">
             <xsl:element name="{name()}">
               <xsl:attribute name="name">
                 <xsl:value-of select="@*"/>
               </xsl:attribute>
               <xsl:for-each select="person">
                 <xsl:sort select="l_name"/>
                 <xsl:copy-of select="."/>
               </xsl:for-each>
             </xsl:element>
         </xsl:for-each>

       </xsl:element>
     </xsl:when>

     <xsl:otherwise>
       <xsl:element name="{name()}">
       <xsl:for-each select="subject">
           <xsl:element name="{name()}">
             <xsl:attribute name="name">
               <xsl:value-of select="@*"/>
             </xsl:attribute>
             <xsl:for-each select="person">
               <xsl:sort select="l_name"/>
               <xsl:copy-of select="."/>
             </xsl:for-each>
           </xsl:element>
       </xsl:for-each>
     </xsl:element>
     </xsl:otherwise>

   </xsl:choose>
 </xsl:template>

</xsl:stylesheet>


<list>
  <subject name=“All">
     <person>
        <l_name>Doe</l_name>
        <f_name>John</f_name>
        <expert/>
        <mail>john(_dot_)doe(_at_)somewhere(_dot_)com</mail>
     </person>
    <person>
        <l_name>Mueller</l_name>
        <f_name>Michael</f_name>
        <expert/>
        <mail>michael(_dot_)mueller(_at_)somewhere(_dot_)com</mail>
     </person>
  </subject>

  <subject name=“A">
     <person>
        <l_name>Doe</l_name>
        <f_name>John</f_name>
        <expert/>
        <mail>john(_dot_)doe(_at_)somewhere(_dot_)com</mail>
     </person>
     <person>
        <l_name>Doe</l_name>
        <f_name>Johanna</f_name>
        <expert/>
        <mail>johanna(_dot_)doe(_at_)somewhere(_dot_)com</mail>
     </person>
  </subject>

  <subject name=“B”>
    <person>
        <l_name>Mueller</l_name>
        <f_name>Michael</f_name>
        <expert/>
        <mail>michael(_dot_)mueller(_at_)somewhere(_dot_)com</mail>
     </person>
     <person>
        <l_name>Mueller</l_name>
        <f_name>Joe</f_name>
        <expert/>
        <mail>joe(_dot_)mueller(_at_)somewhere(_dot_)com</mail>
     </person>
  </subject>
</list>

which produces the requested output except for that in the “All” list is only 
filled the first one of a person which name is appearing multiple times!

So I need to in addition to the key on l_name need to also take the f_name 
into account!

Raimund


GDV Dienstleistungs-GmbH
Glockengießerwall 1
D-20095 Hamburg
www.gdv-dl.de

Sitz und Registergericht: Hamburg
HRB 145291
USt.-IdNr : DE 205183123

Geschäftsführer:
Dr. Jens Bartenwerfer
Michael Bathke

------------------------------------------------------------------
Diese E-Mail und alle Anhänge enthalten vertrauliche und/oder rechtlich 
geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder 
diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den 
Absender und vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die 
unbefugte Weitergabe der E-Mail ist nicht gestattet.

This e-mail and any attached files may contain confidential and/or privileged 
information. If you are not the intended recipient (or have received this 
e-mail in error) please notify the sender immediately and destroy this 
e-mail. Any unauthorised copying, disclosure or distribution of the material 
in this e-mail is strictly forbidden.

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>