[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

GROUPING AND KEYS

Subject: GROUPING AND KEYS
From: Cleyton Jordan <cleytonjordan@xxxxxxxxxxx>
Date: Tue, 28 Apr 2009 03:21:09 -0700 (PDT)
 GROUPING AND KEYS
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>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.