xsl-list
[Top] [All Lists]

Re: Grouping with keys or xpath

2002-10-05 18:54:19
At 2002-10-04 12:11 +0200, Ulf Carlsson wrote:
I only want to group each articlerow in one variant at time.

When I need to do this, I use variables instead of key tables, since variables collect only those nodes to be grouped.

Any hint/help would be appreciated

A solution is below. Note in the algorithm that only the nodes in the collection being grouped are examined for uniqueness and revisited for detail. When not needing a node set of the unique members, this technique is very useful.

I hope this helps.

.................... Ken


T:\ftemp>type ulf.xml
<doc>
        <variant>
                <articlerow>
                        <nev_article_pos>1</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>12</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>12</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <parttitle>Hello World</parttitle>
                <articlerow>
                        <nev_article_pos>18</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>19</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>19</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
        </variant>
        <variant>
                <articlerow>
                        <nev_article_pos>4</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <parttitle>Hello World</parttitle>
                <articlerow>
                        <nev_article_pos>6</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>4</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>18</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>19</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
                <articlerow>
                        <nev_article_pos>19</nev_article_pos>
                        <article>
                                <nev_article>009504145</nev_article>
                        </article>
                </articlerow>
        </variant>
</doc>

T:\ftemp>type ulf.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="doc">
  <document>
    <partinfo>
      <xsl:for-each select="variant">
        <partlist>
          <xsl:variable name="articlerows" select="articlerow"/>
          <xsl:for-each select="$articlerows | parttitle">
            <xsl:if test="self::parttitle">
              <title><xsl:value-of select="."/></title>
            </xsl:if>
            <xsl:if test="generate-id(.)=
                          generate-id($articlerows[nev_article_pos=
                                                current()/nev_article_pos])">
              <partcallout>
                <calloutitem>
                  <xsl:value-of select="nev_article_pos"/>
                  <xsl:for-each select="$articlerows[nev_article_pos=
                                                  current()/nev_article_pos]">
                    <article>
                      <xsl:value-of select="article/nev_article"/>
                    </article>
                  </xsl:for-each>
                </calloutitem>
              </partcallout>
            </xsl:if>
          </xsl:for-each>
        </partlist>
      </xsl:for-each>
    </partinfo>
  </document>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>xt ulf.xml ulf.xsl ulf.out

T:\ftemp>type ulf.out
<?xml version="1.0" encoding="utf-8"?>
<document>
<partinfo>
<partlist>
<partcallout>
<calloutitem>1<article>009504145</article>
</calloutitem>
</partcallout>
<partcallout>
<calloutitem>12<article>009504145</article>
<article>009504145</article>
</calloutitem>
</partcallout>
<title>Hello World</title>
<partcallout>
<calloutitem>18<article>009504145</article>
</calloutitem>
</partcallout>
<partcallout>
<calloutitem>19<article>009504145</article>
<article>009504145</article>
</calloutitem>
</partcallout>
</partlist>
<partlist>
<partcallout>
<calloutitem>4<article>009504145</article>
<article>009504145</article>
</calloutitem>
</partcallout>
<title>Hello World</title>
<partcallout>
<calloutitem>6<article>009504145</article>
</calloutitem>
</partcallout>
<partcallout>
<calloutitem>18<article>009504145</article>
</calloutitem>
</partcallout>
<partcallout>
<calloutitem>19<article>009504145</article>
<article>009504145</article>
</calloutitem>
</partcallout>
</partlist>
</partinfo>
</document>

T:\ftemp>rem Done!


--
G. Ken Holman               mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.        http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0  +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                     Definitive XSLT and XPath
ISBN 0-13-140374-5                             Definitive XSL-FO
ISBN 1-894049-08-X Practical Transformation Using XSLT and XPath
ISBN 1-894049-10-1             Practical Formatting Using XSL-FO
Next public training:          2002-12-08,2003-02-03,06,03-03,06


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



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