Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Grouping based on key result

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 3/6/2009 12:49:00 PM
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@xxxxxxxxxxxxxxxxxxxx
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent