xsl-list
[Top] [All Lists]

Re: [xsl] Grouping & use of keys

2009-02-26 13:41:42
At 2009-02-26 10:21 -0800, Dick Penny wrote:
I am a newbie at XSLT,

Welcome!

have only written 1 identity transform that took me 2
weeks to get working. I have a grouping task for some xml data and plan to
follow either post 49748 by: Martin Honnen
or 49749 by: G. Ken Holman in the 2/24/09 Forum Digest even though I only have
a single group and do not want any item output, just summaries for each
group.

Sounds straightforward. And it sounds like you are dealing with document-wide context. So it sounds to me like using keys would be best. Using the variable-based method of grouping is appropriate when dealing with a sub-document context or a multiple-document context.

I have been scanning/reading the books by Jen Tennison and Mangamo but I guess
I don't understand the use of
<xsl:key name=.........> by Honnen and the
lack of <xsl:key....> by Holman.

In both approaches note the use of the following expression:

   generate-id(.)=
   generate-id( (... some expression...)[1] )

A property of generate-id() is that for every node of every source tree in a given transformation, the string value returned for that node is persistent for the duration of the transform and unique for all nodes.

These properties allow one to confirm that the current node "." is or is not the first node in document order of those nodes addressed by "(...some expression...)[1]".

Such a test is only ever true once for each unique value of nodes being inspected ... that being for the first in document order ... and there is always guaranteed to be at least one such node for each value found in the nodes.

The principle applied in grouping is to do all the heavy lifting only one time for each unique value in the instance, regardless of how many times that value is found in the instance. The above test is only true for the first time that value is found in the instance, not for other times the value is found in the instance.

This gives the stylesheet the opportunity to do the heavy lifting only once for each value and not repeat the heavy lifting at other times for the same value.

I think of <xsl:key......> as a declaration,

It is declaring a property of identified items in the instance that the processor can take advantage of when the stylesheet uses key() to access those items.

similar to "new" in some OO languages.

I don't think of it as such. I think of it as simply a two column table, where the left column has a node and the right column has a corresponding lookup value.

It is either mandatory

It is optional and only added by the stylesheet writer if they wish to take advantage of the key() function to access the identified items. Using key() one supplies the lookup value and gets back the corresponding nodes with that value, in document order.

or not needed.  How can Holman's work with such a declaration?

The <xsl:key> table is not needed in my approach because my approach doesn't involve the use of the key() function.

I'm using the principle described above to find the first item in document order found in a variable for each given value, not in a key table.

When using the key table approach the same principle is being applied, but for all values found document wide.

Note that this principle of doing the heavy lifting for the first in document order of the items with the grouping criteria is inherent in the XSLT 2 grouping facility:

  <xsl:for-each-group  ....grouping-criteria...>

I tell my students to read that as if it were written:

  <xsl:for-the-first-in-document-order-of-each-group  ...grouping criteria...>

because that is where the current context is at at the time you are building the result tree from inside the instruction. It takes some of the mystery away from the instruction.

Note there is yet another approach in XSLT 1, that of using axes, but it is likely the slowest of the approaches available. I cover that, but I show how the other approaches in XSLT 1 are superior, and how the approaches in XSLT 2 are superior to those of XSLT 1.

I hope this has distinguished how there can be multiple differing approaches to the original grouping question.

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

--
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>
--~--

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