RE: [xsl] XSL hold, compare, and replace param value

Subject: RE: [xsl] XSL hold, compare, and replace param value
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 14 Jul 2006 15:53:22 +0100
This is a classic grouping problem which is best tackled using
xsl:for-each-group in XSLT 2.0, or Muenchian grouping (see
http://www.jenitennison.com/xslt/grouping) in 1.0. The 2.0 solution is:


  <xsl:for-each-group select="//entry" group-adjacent="country">
    <h2><xsl:value-of select="current-grouping-key()"/></h2>
    <table>
    <thead>...</thead>
    <tbody>
      <xsl:for-each select="current-group()">
        <tr>
         <td><xsl:value-of select="../@type"/></td>
         <td><xsl:value-of select="name"/></td>
         <td><xsl:value-of select="id"/></td>


Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Steve Sze [mailto:steveyksze@xxxxxxxxx] 
> Sent: 14 July 2006 15:23
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] XSL hold, compare, and replace param value
> 
> Hi Spencer and Mike
> 
> Sorry for not giving you the details of my output.  Im 
> looking to display the title for every new country.
> 
> So, the html output will look like this:
> 
> USA
> Group     Name        id
> AAA       Adel          12345
> AAA       Barry         12346
> AAA       Carl           12347
> BBB       Dave          12345
> BBB       Ethel          12346
> BBB       Fred           12347
> 
> EUR
> Group    Name         id
> CCC      George       24567
> CCC      Harold        23458
> CCC      Jennifer      23459
> 
> Hope this helps, thanks again!
> Steve
> 
> ===========================================
> 
> Sample XML file
> <report>
>       <column>name</column>
>       <column>country</column>
>       <column>group</column>
>       <table>
>               <date>07-13-2006
>                       <group type="AAA">111111
>                               <entry>
>                                       <name>Adel</name>
>                                       <country>USA</country>
>                                       <id>12345</id>
>                               </entry>
>                               <entry>
>                                       <name>Barry</name>
>                                       <country>USA</country>
>                                       <id>12346</id>
>                               </entry>
>                               <entry>
>                                       <name>Carl</name>
>                                       <country>USA</country>
>                                       <id>12347</id>
>                               </entry>
>                       </group>
>                       <group type="BBB">111111
>                               <entry>
>                                       <name>Dave</name>
>                                       <country>USA</country>
>                                       <id>12345</id>
>                               </entry>
>                               <entry>
>                                       <name>Ethel</name>
>                                       <country>USA</country>
>                                       <id>12346</id>
>                               </entry>
>                               <entry>
>                                       <name>Fred</name>
>                                       <country>USA</country>
>                                       <id>12347</id>
>                               </entry>
>                       </group>
>                       <group type="CCC">111111
>                               <entry>
>                                       <name>George</name>
>                                       <country>EUR</country>
>                                       <id>24567</id>
>                               </entry>
>                               <entry>
>                                       <name>Harold</name>
>                                       <country>EUR</country>
>                                       <id>23458</id>
>                               </entry>
>                               <entry>
>                                       <name>Jennifer</name>
>                                       <country>EUR</country>
>                                       <id>23459</id>
>                               </entry>
>                       </group>
>               </date>
>       </table>
> </report>
> 
> ===========================================
> 
> Hope this is what you're looking for:
> 
> When I run this XSL:
> 
> <?xml version='1.0'?>
> <xsl:stylesheet version="1.0" 
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> 
> <xsl:template match="/">
>       <xsl:apply-templates/>
> </xsl:template>
> 
> <xsl:template match="*">
> <xsl:copy>
>       <xsl:copy-of select="@*"/>
>       <xsl:apply-templates/>
> </xsl:copy>
> </xsl:template>
> 
> <xsl:template match="country[parent::entry/following-sibling::entry]">
> <xsl:variable name="nextCountry"
> select="parent::entry/following-sibling::entry[1]/country"/>
> <xsl:choose>
> <xsl:when test="$nextCountry = .">
> <country><xsl:apply-templates/></country>
> </xsl:when>
> <xsl:otherwise>
> <country><xsl:value-of select="$nextCountry"/></country> 
> </xsl:otherwise> </xsl:choose> </xsl:template>
> 
> <xsl:template 
> match="country[not(parent::entry/following-sibling::entry)]">
> <xsl:variable name="nextCountry"
> select="ancestor::group/following-sibling::group[1]/entry[1]/c
> ountry"/>
> <xsl:choose>
> <xsl:when test="$nextCountry = . or not($nextCountry)"> 
> <country><xsl:apply-templates/></country>
> </xsl:when>
> <xsl:otherwise>
> <country><xsl:value-of select="$nextCountry"/></country> 
> </xsl:otherwise> </xsl:choose> </xsl:template>
> 
> </xsl:stylesheet>
> 
> Against your XML above, I get Fred's Country Changing as he 
> is the only one who's next country differs,,, Am I reading 
> you correctly?
> 
> Spencer Tickner
> 
> On 7/13/06, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > > I have a problem holding on to a param or variable value in xslt.
> > > Setting it Globally or Locally.
> > >
> > > How can I hold onto the country value? Compare to the 
> next value, if 
> > > country is diff. then replace the value for that param 
> with the next 
> > > country value?
> >
> > Please take a step back. Try to describe the problem: what's the 
> > input, what's the required output?
> >
> > It looks to me as if you are trying to code a solution using ideas 
> > learned from other programming languages, which might not 
> be the right 
> > way to tackle the problem in XSLT.
> >
> > Michael Kay
> > http://www.saxonica.com/

Current Thread