Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old June 20th, 2012, 03:44 AM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT - row numbers which met criteria

Hi. I have a question how I can get something like sequencer or row number of elements which met criteria.

example: I have 5 elements with parameter start date like this
<phase type="Available" start="01.12.2012"/>

2 of them met criteria for example year is equal to actual year. I want to have some variable starts from 0, so in this case I will have 2 phases with ID 0 and 1.

Thanks in advance.
 
Old June 20th, 2012, 04:15 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Depending on the overall processing structure (what happens to the nodes that don't meet the criteria?) the solution will either use position() or xsl:number. If you are only processing the nodes that meet the criteria, use

Code:
<xsl:for-each select="phase[ends-with(@year, '2012')]">
   <xsl:value-of select="position()"/>
   ...
If you need to process the other nodes as well, try

Code:
<xsl:number count="phase[ends-with(@year, '2012')]">
with level="simple" if the nodes are all siblings, or level="any" otherwise.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 20th, 2012, 05:33 AM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for quick replay :)

I need to set x position based on sequence number in group which met criteria. I cant use position() in this case


pseudo code:
if(year = acutal)
//1 element
<react x=0
//2 element
<rect x = 1
else if(year = year -1)
//1 element
<react x= 0
//2 element
<rect x = 1
//2 element
<rect x = 2
else if(year = year+1)
...

structure look like this:

<project type="Migration" name="Test">
<phase end="31.03.2018" type="Deprecated" start="31.03.2015"/>
<phase end="31.12.2013" type="Available" start="01.12.2012"/>
<phase end="30.11.2012" type="Migration" start="01.01.2012"/>
<phase end="30.11.2011" type="PoC" start="01.01.2011"/>
<phase end="31.10.2010" type="Evaluation" start="01.09.2010"/>
<phase end="31.10.2009" type="Migration" start="01.09.2009"/>
</project>

XSL look like
<xsl:template match="phase">
<xsl:choose>
<xsl:when test="$start=$end and date:year($start) = $year">
<polyline points="{$X}, {$Y} {$X+$project-font-size}, {$Y} {$X+$project-font-size * 1.5}, {$Y + $project-font-size * 0.5} {$X+$project-font-size}, {$Y + $project-font-size} {$X}, {$Y + $project-font-size} {$X}, {$Y}" class="phase {./@type}"/>
</xsl:when>
<xsl:when test="(date:year($start) &lt; ($year - 1 )) and (date:year($end) &lt; ($year - 1 ))">
<xsl:variable name="row_number">
//here I need for element 1 have 1, for element 2 value 2 etc - that each next element will be next to prevous
</xsl:variable>
<path class="phase {./@type}" d="m{row_number},{$Y + $project-font-size}.0,0l1.09375,-1.09375l-1.09375,-1.09375l-1.3125,0l1.03125,1.03125l-1.03125,1.15625z" />
</xsl:when>
<xsl:when test="(date:year($start) &gt; ($year + 1 )) and (date:year($end) &gt; ($year + 1 ))">
<path class="phase {./@type}" d="m162.5,{$Y + $project-font-size}.0,0l1.09375,-1.09375l-1.09375,-1.09375l-1.3125,0l1.03125,1.03125l-1.03125,1.15625z" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="W" select="$scale-another-year * ($dayslastyear + $daysnextyear) + $daysthisyear * $scale-this-year"/>
<rect x="{$X}" y="{$Y}" width="{$W}" height="{$project-font-size}" class="phase {./@type}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Last edited by lonas; June 20th, 2012 at 06:22 AM..
 
Old June 20th, 2012, 07:10 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I've no idea what your pseudocode means. "if (year = year + 1)" makes no sense to me. Please show the output you require for this input.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 20th, 2012, 07:25 AM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry,
if (year = year + 1) means start date year = actual year +1

I need to have consecutive numbers of element met criteria starts from 0.

Last edited by lonas; June 20th, 2012 at 07:27 AM..
 
Old June 21st, 2012, 04:46 AM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried also something like that:

Code:
 <xsl:variable name="row_number" select="count(./preceding-sibling::line[@type='phase'])+1" />
but it doesnt work:





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ajax change textbox bgcolor after Criteria met mkthompson Ajax 1 July 28th, 2008 08:56 AM
how to generate random numbers in xslt anboss XSLT 3 July 23rd, 2008 09:10 AM
XSLT Row replication in Tables jsuen XSLT 1 August 7th, 2006 11:07 AM
Grouped and sorted table row numbers ADenny XSLT 1 April 24th, 2006 10:06 AM
Sum of random numbers in a table with XSLT Tschuri XSLT 0 May 8th, 2005 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.