[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

sort on one key - duplicates

Subject: sort on one key - duplicates
From: "Raimund Kammering raimund.kammering@xxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 7 Mar 2017 09:31:32 -0000
 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 Ibm lacking the idea to accomplish this! The XML, XSL
looks like this:

<list>
   <subject name=bA">
      <person>
         <l_name>Doe</l_name>
         <f_name>John</f_name>
         <expert/>
         <mail>john.doe@xxxxxxxxxxxxx</mail>
      </person>
      <person>
         <l_name>Doe</l_name>
         <f_name>Johanna</f_name>
         <expert/>
         <mail>johanna.doe@xxxxxxxxxxxxx</mail>
      </person>
   </subject>

   <subject name=bBb>
     <person>
         <l_name>Mueller</l_name>
         <f_name>Michael</f_name>
         <expert/>
         <mail>michael.mueller@xxxxxxxxxxxxx</mail>
      </person>
      <person>
         <l_name>Mueller</l_name>
         <f_name>Joe</f_name>
         <expert/>
         <mail>joe.mueller@xxxxxxxxxxxxx</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=bAll">
      <person>
         <l_name>Doe</l_name>
         <f_name>John</f_name>
         <expert/>
         <mail>john.doe@xxxxxxxxxxxxx</mail>
      </person>
     <person>
         <l_name>Mueller</l_name>
         <f_name>Michael</f_name>
         <expert/>
         <mail>michael.mueller@xxxxxxxxxxxxx</mail>
      </person>
   </subject>

   <subject name=bA">
      <person>
         <l_name>Doe</l_name>
         <f_name>John</f_name>
         <expert/>
         <mail>john.doe@xxxxxxxxxxxxx</mail>
      </person>
      <person>
         <l_name>Doe</l_name>
         <f_name>Johanna</f_name>
         <expert/>
         <mail>johanna.doe@xxxxxxxxxxxxx</mail>
      </person>
   </subject>

   <subject name=bBb>
     <person>
         <l_name>Mueller</l_name>
         <f_name>Michael</f_name>
         <expert/>
         <mail>michael.mueller@xxxxxxxxxxxxx</mail>
      </person>
      <person>
         <l_name>Mueller</l_name>
         <f_name>Joe</f_name>
         <expert/>
         <mail>joe.mueller@xxxxxxxxxxxxx</mail>
      </person>
   </subject>
</list>

which produces the requested output except for that in the bAllb 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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.