Hi all, I'm sure a variation of this has been asked before but I haven't found the exact solution yet.
I need to limit the output of my nodes in a macro based firstly on date (this I can do successfully) but would only like 4 items output.
In other words, there may be 9 items in the list, the first 2 and the 4th may be out of date so I'd like to see the 3rd, 5th, 6th and 7th in the list.
So for the following XML
<list> <item1 date="1 Mar 2010">1</a> <item2 date="2 Mar 2010">2</a> <item3 date="10 Mar 2010">3</a> <item4 date="4 Mar 2010">4</a> <item5 date="13 Mar 2010">5</a> <item6 date="14 Mar 2010">6</a> <item7 date="18 Mar 2010">7</a> <item8 date="21 Mar 2010">8</a> <item9 date="31 Mar 2010">9</a> </list>
I'd expect to see
<output> <item3 date="10 Mar 2010">3</a> <item5 date="13 Mar 2010">5</a> <item6 date="14 Mar 2010">6</a> <item7 date="18 Mar 2010">7</a> </output>
Sure it's an easy one for an experienced XSLT coder. (Don't worry about the date code - I have that completed already but I am getting a list that's still more than 4 items long)
Will this not give the position of the node in scope? What if that node is to be excluded based on the date? Will it still be counted or will the position() refer to the position of the nodes added in to the output??
I have a feeling that position() will report the position of the node in scope...
Your feeling is right, part of the way. Normally, in XSLT, if you have a template for an element, and you use the apply-templates instruction to output these, you'd get the position of the node in the original XML document when using position(), even if you use the sort instruction to reorder the elements. The for-each instruction is your friend here, because it will change the context and give you the new position() values.
So you want to use for-each to select the nodes in question and sort them by date, then test the value of position() before applying templates - something like this pseudo-code:
Hi Chriztian - thanks for the reply and apologies for the delay in replying myself.
The above situation that you stated makes sense, but I'm still a bit confused about how to limit the OUTPUT number of nodes to 4. My exact context is that I have a number of News nodes - and I only wish to display the current (i.e. news relevant to today or later) nodes on the page.
So I will need an AND statement in my <xsl:for-each... for example (pseudo-code)
<xsl:template match="/"> <xsl:for-each select="$currentPage//node[@nodeTypeAlias = 'News']" AND "umbraco.library:DateGreaterThanOrEqualToday(current()/data [@alias='newsDate'])"> <xsl:sort select="data" order="ascending" /> <xsl:if test="position() <= 4"> OUTPUT HERE etc...
Anad that's not possible, as far as I'm aware.
Does that make more sense of what I am trying to achieve?
Limiting output to a certain number of nodes
Hi all, I'm sure a variation of this has been asked before but I haven't found the exact solution yet.
I need to limit the output of my nodes in a macro based firstly on date (this I can do successfully) but would only like 4 items output.
In other words, there may be 9 items in the list, the first 2 and the 4th may be out of date so I'd like to see the 3rd, 5th, 6th and 7th in the list.
So for the following XML
<list>
<item1 date="1 Mar 2010">1</a>
<item2 date="2 Mar 2010">2</a>
<item3 date="10 Mar 2010">3</a>
<item4 date="4 Mar 2010">4</a>
<item5 date="13 Mar 2010">5</a>
<item6 date="14 Mar 2010">6</a>
<item7 date="18 Mar 2010">7</a>
<item8 date="21 Mar 2010">8</a>
<item9 date="31 Mar 2010">9</a>
</list>
I'd expect to see
<output>
<item3 date="10 Mar 2010">3</a>
<item5 date="13 Mar 2010">5</a>
<item6 date="14 Mar 2010">6</a>
<item7 date="18 Mar 2010">7</a>
</output>
Sure it's an easy one for an experienced XSLT coder. (Don't worry about the date code - I have that completed already but I am getting a list that's still more than 4 items long)
Thanks
Sean
Will this not give the position of the node in scope? What if that node is to be excluded based on the date? Will it still be counted or will the position() refer to the position of the nodes added in to the output??
I have a feeling that position() will report the position of the node in scope...
Hi Sean,
Your feeling is right, part of the way. Normally, in XSLT, if you have a template for an element, and you use the apply-templates instruction to output these, you'd get the position of the node in the original XML document when using position(), even if you use the sort instruction to reorder the elements. The for-each instruction is your friend here, because it will change the context and give you the new position() values.
So you want to use for-each to select the nodes in question and sort them by date, then test the value of position() before applying templates - something like this pseudo-code:
Hope it helps,
/Chriztian
Hi Chriztian - thanks for the reply and apologies for the delay in replying myself.
The above situation that you stated makes sense, but I'm still a bit confused about how to limit the OUTPUT number of nodes to 4. My exact context is that I have a number of News nodes - and I only wish to display the current (i.e. news relevant to today or later) nodes on the page.
So I will need an AND statement in my <xsl:for-each... for example (pseudo-code)
Anad that's not possible, as far as I'm aware.
Does that make more sense of what I am trying to achieve?
Thanks,
Sean
Hi Sean,
yes you can do this, try this:
HTH,
Peter
Perfect - thanks Peter, and the others who got me there eventually.
is working on a reply...