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 4th, 2005, 03:18 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with" i=i+1" and calculating time

Hi!

I am having two problems in XSLT.

Based on the information in a xml-file, I want to make route-tables for bus-journeys including the bus-stops and the time the bus arrives each bus-stop.

Problem 1)

In my XSL-file I have the line:

<xsl:variable name="varStopLink" select="/route/StopLink[<..some criteria..>]"/>


part of zml-file:
<route>
<StopLink stoplinkid="1" fromstopid="157" tostopid="158" runtime="35" />
<StopLink stoplinkid="2" fromstopid="158" tostopid="159" runtime="72"/>
<StopLink stoplinkid="3" fromstopid="159" tostopid="160" runtime="32"/>
<StopLink stoplinkid="4" fromstopid="160" tostopid="129" runtime="56"/>
<StopLink stoplinkid="5" fromstopid="129" tostopid="130" runtime="76"/>
<StopLink stoplinkid="6" fromstopid="133" tostopid="134" runtime="41"/>
<StopLink stoplinkid="7" fromstopid="161" tostopid="162" runtime="45"/>
</route>


In the variable name="varStopLink" I have some <Stoplink>-elements (I don't know which or how many!!). Based on the <Stoplink>s picked, I want to calculate the time the bus arrives each busstop.
- The time the bus arrives the first busstop is the departuretime + the runtime for the first stoplink picked
- The time the bus arrives the second busstop is the departuretime + the runtime for the first stoplink picked + the runtime for the first stoplink picked
- ......

In the Java programming language I could have used i=deptime + i, but I don't know how to do this i XSLT

Problem 2)

The departuretime is written on the form <hour:minute:seconds>

How do you add the seconds calculated above to the departuretime.
If the departuretime is 06:30:00 and the runtime from the first busstop to the second is 65 seconds, I want the arrival at the second bus-stop to be 06:31:05

Hope anyone out there can help me on this. This is the last problem to solve before my boss is happy!!!

:)



 
Old June 4th, 2005, 04:35 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I hope your buses are more punctual than ours in Reading.

You can calculate the time at each stop as

<xsl:for-each select="$varStopLink">
  <arrival time="$start-time + sum(preceding-sibling::StopLink/@runtime) + @runtime"/>
</xsl:for-each>

This probably has quadratic performance, and a recursive solution might be faster, but would be more complex.

Your second problem can be easily solved with XSLT 2.0, which has types and operators for handling times and durations. In XSLT 1.0 you have to parse the hh:mm:ss value yourself (your buses are timed to the second?) for example using something like

substring($time, 1, 2) * 3600 + substring($time, 4, 2) * 60 + substring($time, 6, 2)



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 5th, 2005, 04:05 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!

Thank you for your answer. I learned a lot of what you wrote, but it didn't completely solve my problem. I don't think I described it good enough!!

Problem 1)

The function sum(preceding-sibling::StopLink/@runtime) calculates (as I understand) the total runtime for ALL the <Stoplink>s listed before the current <StopLink>. The problem is that sometimes just some of the <StopLink>s listed before are included.

For example in one journey, the <StopLink>s with the stoplinkid's 1,2,4,5,7 are part of the journey. Using the sum(preceding-sibling::StopLink/@runtime) gave me the right answer for the first two calculations ,but wrong answer for the last three since the <StopLink>s with id = 3 and 6 are excluded and not part of the journey.

Problem 2)

Here I have almost solved the problem using the function you gave me. When I added the departuretime for one journey and added the runtime to the first stop (05:30:00 + 35) I got a total of 19800 seconds. The problem appeared when I wanted to recalculate 19800 to the form hh:mm:ss. I got 5:30:0 (5 hours,30 minutes an 0 seconds), but I want it to be 05:30:00. I want it to be exactly two digits before and after both the semicolons. The problem is to deside (in the code at runtime)when to add an extra 0 in time-expression (hh:mm:ss) and how to do it.

1)If the result is 17:34:12 no 0 is to be added.
2)If the result is 13:4:3 two 0 is to be added. One for the minutes (should be 04) and one for the seconds (03).

I have twisted my head today, but I didn't find any good solutions. Sorry if I bother you, but I really want this to be right





 
Old June 5th, 2005, 04:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

For problem one, just add the selection criteria (which you omitted from your posting):

sum(preceding-sibling::StopLink[<some criteria>])

For the second problem, use format-number($x, '00') to format a number as two decimal digits, with a leading zero if necessary.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 6th, 2005, 04:17 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi again!!!:)

Problem 2 solved!!! Thank you very much!!!!!!!!!!!

I have still some problems with number 1. I didn't work when I added the selection criteria. May be I am a bit slow here (this is my fi rst XSLT experience), but the structure of the xml-file is like this:

<JourneyPatternDetails journeypatternid="1" stoplinkid="1"/>
<JourneyPatternDetails journeypatternid="1" stoplinkid="2"/>
<JourneyPatternDetails journeypatternid="1" stoplinkid="3"/>
<JourneyPatternDetails journeypatternid="1" stoplinkid="4"/>
<JourneyPatternDetails journeypatternid="1" stoplinkid="5"/>
<JourneyPatternDetails journeypatternid="2" stoplinkid="1"/>
<JourneyPatternDetails journeypatternid="2" stoplinkid="2"/>
<JourneyPatternDetails journeypatternid="2" stoplinkid="3"/>
<JourneyPatternDetails journeypatternid="2" stoplinkid="37"/>
<JourneyPatternDetails journeypatternid="2" stoplinkid="6"/>
<JourneyPatternDetails journeypatternid="3" stoplinkid="7"/>
<JourneyPatternDetails journeypatternid="4" stoplinkid="8"/>
<JourneyPatternDetails journeypatternid="5" stoplinkid="9"/>
<JourneyPatternDetails journeypatternid="5" stoplinkid="10"/>
<JourneyPatternDetails journeypatternid="5" stoplinkid="11"/>

<StopLink stoplinkid="1" fromstopid="157" tostopid="158" runtime="35"/>
<StopLink stoplinkid="2" fromstopid="158" tostopid="159" runtime="35"/>
<StopLink stoplinkid="3" fromstopid="159" tostopid="160" runtime="32"/>
<StopLink stoplinkid="4" fromstopid="160" tostopid="129" runtime="29"/>
<StopLink stoplinkid="5" fromstopid="129" tostopid="130" runtime="76"/>
<StopLink stoplinkid="6" fromstopid="133" tostopid="134" runtime="41"/>
<StopLink stoplinkid="7" fromstopid="161" tostopid="162" runtime="45"/>
<StopLink stoplinkid="8" fromstopid="135" tostopid="137" runtime="35"/>
<StopLink stoplinkid="9" fromstopid="145" tostopid="147" runtime="35"/>
<StopLink stoplinkid="10" fromstopid="147" tostopid="149" runtime="35"/>
<StopLink stoplinkid="11" fromstopid="149" tostopid="151" runtime="35"/>
<StopLink stoplinkid="37" fromstopid="160" tostopid="133" runtime="35"/>

All the <JourneyPatternDetails> nodes with the same journeypatternid describes a bus-journey. All <JourneyPatternDetails> nodes has a stoplinkid attribute. Taking the value from this stoplinkid , I want to go to the <StopLink> node with that stoplinkid and find that stoplinks runtime. Doing this for all of the journeys stoplinkids it is possibles to find the time the bus arrives each busstop.A <StopLink> contains a frombusstop and a tobusstop (fromstopid and tostopid).

For example the bus-Journey with the journeypatternid = 2, is described by the <StopLink> nodes with stoplinkids=1,2,3,37 and 6

The bus drives the <StopLink> with stoplinkid=1 first, then the <StopLink> with stoplinkid=2 ........ and finally the <StopLink> with stoplinkid=6.

An other part of the xml-file gives us the departuretime for the journeys and based on the information above I want to calculate the time the bus arrives each busstop.

This is my last problem until everything is solved and the people in parts of Norway and Sweden can find out when their next bus arrives the bus-stops:)!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating time Marta Access 0 April 15th, 2008 10:34 PM
Calculating a time difference SQLScott SQL Server 2005 11 October 25th, 2007 02:42 AM
calculating time differences vincee SQL Server 2000 6 January 24th, 2006 11:02 AM
Calculating Time elapsed on form load Brendan Bartley Access 0 June 21st, 2005 09:02 AM
Missing time part while calculating the Delay happygv SQL Language 2 October 29th, 2003 11:57 PM





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