Looking for a solution for checking a current value against a previous value while inside a for-each loop.
This is an open discussion with 3 replies, filed under XSLT.
Search
however I would like the text "2011" to appear in my XML output only once, at the top of all 2011 projects
Can you explain what you mean with your XML output? Is that the XML output that you run the XSLT template against?
what I would normally do is check the current project's year against the year of the previous project in the loop.
You might want to look into templating and XPath selectors. What you want to achieve can be done by applying a template to all nodes that match a certain year.
So my suggestion would be to start with reading up on how templating works, then move over to xpath and last but not least look into recursive templates (which allow you to create a distinct list of available years that you can use to loop over).
Try grouping your datasource by the date field. That should output the xml like you described it...
@remie you were right. I discovered a simple solution using the Xpath "preceding" axis selector.
So comparing the value of a current note's attribute to a previous node's attribute in a for-each loop is as simple as this:
<xsl:for-each select="data/projects/entry"> <xsl:if test="preceding::year[1] = year"> <h1>This node's "year" attribute matches the previous node's "year" attribute</h1> </xsl:if> </xsl:for-each>
And you are right. I need to learn more about templates and selectors. So many possibilities.
Thanks to everyone for their help as always!
Matt
Create an account or sign in to comment.
Hello,
I have a data set that's a list of projects. Each project has a year, client, title, description, etc., and these projects are sorted by date. I'd like to reduce redundancies. For example I have about 10 projects under 2011, however I would like the text "2011" to appear in my XML output only once, at the top of all 2011 projects, and so on for each consecutive year.
I come from a javascript / actionscript / php background, so what I would normally do is check the current project's year against the year of the previous project in the loop. Something like this:
http://pastie.org/4027231
However I've learned that XSL does not permit overwriting of params or variables. I'm sure there's another solution, I just can't figure it out.
Much thanks!
Matt