xslt transform with a count
I'm trying to perform an xslt transform on some xml data into html. There are 3 tasks that开发者_JAVA技巧 this transform needs to do and these are:
- Sort data by date
- Output only those with a certain id
- Output only 3 of those items
So for example a snippet of my data looks like this:
<program id="brand_id_1">
<date>2011-10-25</date>
<some_info>This is some info</some_info>
</program>
<program id="brand_id_2">
<date>2011-10-22</date>
<some_info>This is some info</some_info>
</program>
<program id="brand_id_1">
<date>2011-10-27</date>
<some_info>This is some info</some_info>
</program>
I can order by date, I can make sure I output only the ones with the id brand_id_1, but how do I stop outputting once I've done this 3 times?
Any help, much appreciated! Helen
Sort and then check the position as for instance in the following sample:
<xsl:for-each select="//program[@id = 'brand_id_1']">
<xsl:sort select="date" data-type="text"/>
<xsl:if test="position() < 4">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
You can use a template and recursion to effectively create a for loop however as xslt is really just transformation tool the best thing if you can is to modify your source xml
See the answer here xsl recursive loop node by index
精彩评论