xsl-list
[Top] [All Lists]

Re: [xsl] Grouping based on key result

2009-03-06 07:48:47
At 2009-03-06 10:05 +0530, Ganesh Babu N wrote:
Can somebody respond to my problem. Please.

Very few straightforward changes are needed to your original stylesheet. You just have to move the determination for the logic you are using inside your loop to outside your loop where you can make the determination accurately.

I also took a moment to distill out the common code from inside your choose statement, which is a pattern that often helps with downstream processing (but had nothing to do with your problem).

I hope the example below helps.  I added one variable and two parameters.

. . . . . . . Ken


T:\ftemp>type ganesh.xml
<author-group>
  <author>
     <given-name>T.</given-name>
     <surname>Miwa</surname>
     <cross-ref refid="aff2"><sup>b</sup></cross-ref>
  </author>
  <author>
     <given-name>T.</given-name>
     <surname>Nomura</surname>
     <cross-ref refid="aff2"><sup>b</sup></cross-ref>
  </author>
  <author>
      <given-name>J.</given-name>
      <surname>Sakakibara</surname>
      <cross-ref refid="aff1"><sup>a</sup></cross-ref>
  </author>
  <author>
      <given-name>H.</given-name>
      <surname>Shintani</surname>
      <cross-ref refid="aff1"><sup>a</sup></cross-ref>
  </author>
  <author>
   <initials>B.E.</initials>
   <given-name>B.E.</given-name>
   <surname>Ungerechts</surname>
   <cross-ref refid="aff3"><sup>c</sup></cross-ref>
  </author>

  <affiliation id="aff1"><label>a</label><textfn>Graduate School of
Systems and Information Engineering, University of Tsukuba, Tsukuba
305-8573, Japan</textfn></affiliation>

  <affiliation id="aff2"><label>b</label><textfn>Graduate School of
Comprehensive Human Sciences, University of Tsukuba, Tsukuba 305-8573,
Japan</textfn></affiliation>

  <affiliation id="aff3"><label>c</label><textfn>Institute of Sport
Sciences, University of Bielefeld, D-33615 Bielefeld,
Germany</textfn></affiliation>
</author-group>

T:\ftemp>call xslt2 ganesh.xml ganesh.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<author>T. Miwa</author>, <author>T. Nomura</author>, <author>J. Sakakibara</author>, <author>H. Shintani</author>
     (Japan)
    and <author>B.E. Ungerechts</author>
     (Germany)

T:\ftemp>type ganesh.xsl
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
              xmlns:xs="http://www.w3.org/2001/XMLSchema";
              xmlns:f="data:,f"
              exclude-result-prefixes="xs f">

<xsl:output indent="yes" encoding="US-ASCII"/>
<xsl:strip-space elements="*"/>

<xsl:key name="country" match="affiliation/textfn"
use="parent::affiliation/@id"/>
<xsl:template match="author-group">
   <xsl:variable name="multiple-authors" select="count(author)>1"/>
   <xsl:for-each-group select="author" group-adjacent="f:country(.)">
      <xsl:apply-templates select="current-group()">
        <xsl:with-param name="multiple-authors" select="$multiple-authors"/>
        <xsl:with-param name="last-group" select="position()=last()"/>
      </xsl:apply-templates>
     (<xsl:value-of select="current-grouping-key()"/>)
   </xsl:for-each-group>

</xsl:template>

<xsl:template match="author">
<xsl:param name="multiple-authors" as="xs:boolean"/>
<xsl:param name="last-group" as="xs:boolean"/>
 <xsl:choose>
  <xsl:when test="position() = last() and $last-group and $multiple-authors">
     <xsl:text> and </xsl:text>
   </xsl:when>
  <xsl:when test="position() != 1">
     <xsl:text>, </xsl:text>
   </xsl:when>
 </xsl:choose>
 <author><xsl:value-of select="given-name,surname"/></author>
</xsl:template>

<xsl:function name="f:country" as="xs:string">
 <xsl:param name="a" as="element()"/>
 <xsl:choose>
 <xsl:when test="$a/cross-ref">
 <xsl:sequence select="key('country',
$a/cross-ref/@refid,root($a))/tokenize(.,',\s+')[last()]"/>
 </xsl:when>
 <xsl:otherwise>
   <xsl:sequence
select="$a/following-sibling::affiliation[1]/tokenize(.,',\s+')[last()]"/>
 </xsl:otherwise>
 </xsl:choose>
</xsl:function>

</xsl:stylesheet>
T:\ftemp>rem Done!



--
XQuery/XSLT training in Prague, CZ 2009-03 http://www.xmlprague.cz
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--