 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 30th, 2006, 02:46 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to look at the previous record in a for-each
Is it possible to, while looping through a for-each, look at the previous record's value?
So that if I'm at position 2 and want to see the value of "symbol" for position 1, can this be done within the loop?
If no, how else can I accomplish this?
sorry for the noob question.
|
|

October 30th, 2006, 03:01 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
An xsl:for-each is not a loop, all nodes in the sequence are processed in parallel, in an ideal world anyway...
If you show the structure of your XML and what you need they're will most likely be a simple XPath expression to retrieve the node you want.
--
Joe ( Microsoft MVP - XML)
|
|

October 30th, 2006, 03:37 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
<EZMessage action="tool.coveredcall">
<data>
<spread>
<symbol>SIRF</symbol>
<expString>Oct 06</expString>
<expTime>1161406800000</expTime>
<returnRate>2.637</returnRate>
<stockPrice>22.18</stockPrice>
<callPrice>1.10</callPrice>
<industryName>Semiconductors</industryName>
<spc>100.0</spc>
<leg>
<side>B</side>
<securityKey>SIRF:::S</securityKey>
</leg>
<leg>
<side>S</side>
<securityKey>QIR:20061021:225000:C</securityKey>
</leg>
</spread>
<spread>
<symbol>MLS</symbol>
<expString>Oct 06</expString>
<expTime>1161406800000</expTime>
<returnRate>2.157</returnRate>
<stockPrice>15.57</stockPrice>
<callPrice>0.95</callPrice>
<industryName>Real Estate</industryName>
<spc>100.0</spc>
<leg>
<side>B</side>
<securityKey>MLS:::S</securityKey>
</leg>
<leg>
<side>S</side>
<securityKey>MLS:20061021:150000:C</securityKey>
</leg>
</spread>
...
</data>
</EZMessage>
basically I want to NOT display the node if the Symbol is the same as the previous node...
|
|

October 30th, 2006, 04:10 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>I want to NOT display the node if the Symbol is the same as the previous node...
That's then a classic grouping problem, read all about it at http://www.jenitennison.com/xslt/grouping
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

October 30th, 2006, 04:22 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you :)
|
|

October 30th, 2006, 04:33 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
<xsl:key name="contacts-by-surname" match="contact" use="surname" />
<xsl:template match="records">
<xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
<xsl:sort select="surname" />
<xsl:value-of select="surname" />,<br />
<xsl:for-each select="key('contacts-by-surname', surname)">
<xsl:sort select="forename" />
<xsl:value-of select="forename" /> (<xsl:value-of select="title" />)<br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Oye... This is enough to make a newbie's head explode.
|
|

October 30th, 2006, 04:35 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
I think that counts as a grouping problem, displaying data from the first occurence of each symbol. If using XSLT 1.0 then see the first post in this forum for links on grouping.
--
Joe ( Microsoft MVP - XML)
|
|
 |