xsl-list
[Top] [All Lists]

[xsl] GROUPING AND KEYS

2009-04-28 06:21:41

Hi,

I would like to understand how keys and grouping work in XSLT and I came across 
a post that might help me. However, I have some questions regarding the code 
and would appreciate if someone could shed some light about sections of the 
code I do not understand.

"I have Nodes that all have "Shift" data.  Within these Shifts, I have a Part 
Number.  Now, I want to be able to group and add together all the data 
associated with a Part Number, regardless of what Shift it was produced in. 

For example, I want to be able to add together all the "counts" for part number 
1. " 

<foo>
<shift>
  <partNo num="1">
    <data count="2"/>
    <data time="3"/>
  </partNo>
</shift>
<shift>
  <partNo num="1">
    <data count="1"/>
    <data time="5"/>
  </partNo>
</shift>
<shift>
  <partNo num="2">
    <data count="5"/>
    <data time="7"/>
  </partNo>
</shift>
<foo>
1 - Is there a way to visualise in XSLT what this key looks like?

<xsl:key name="partNo" match="partNo" use="@num" />


In this statement <xsl:for-each select="foo/shift/partNo[count(. | 
key('partNo', @num)[1]) = 1]">

we are selecting all partNo nodes where count(. | key( 'partNo', @num)[1]) = 1]

2 - What does . mean in this expression? the partNo node? 

3 - What does | mean? 

4 - key( 'partNo', @num) - this is the key. Are we using the current @num 
attribute to get all the partNo whose @num = 1 then 2 etc...?

5 - )[1]) = 1 - Why is it using an array index [1] to get the first one and 
then using = 1 ?

I would very much appreciate some feedback and help to understand this code..

CJ


CODE

<xsl:key name="partNo" match="partNo" use="@num" />

<xsl:template match="/">
    <xsl:for-each select="foo/shift/partNo[count(. | key('partNo', @num)[1]) = 
1]">
        <xsl:sort select="@num" />
        Part num: <xsl:value-of select="@num" />
        <xsl:for-each select="key('partNo', @num)">
            [<xsl:value-of select="data/@count" />,
            <xsl:value-of select="data/@time" />]
        </xsl:for-each>
        <br />

    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>





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