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 October 28th, 2009, 10:52 AM
Registered User
 
Join Date: Oct 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding values from 2 different nodes

Hi everyone.

I have the following XML file and XSLT file. They already process fine, but I need to sum the attribute values from the 2 first nodes.
More specific, I need to make the sum of Delay hours of the first and second Traveltime XML nodes, the sum of OptimalTT of the first and second node, the sum of ActualTT of the first and second node. Currently I use the totalseconds value which I divide by 60, then round.


Code:
<?xml version="1.0" encoding="utf-8"?>
<TravelTimeDocument>
  <Header>
    <DateCreated years="2009" months="10" days="22" hours="7" minutes="7" seconds="34" fulltext="2009-10-22T07:07:34.501875Z" />
    <Language tag="fr" isocode="fr-BE" isonum="1036" />
  </Header>
  <TravelTimes>
    <TravelTime Id="1002575" Name="A602_CheneeLoncin" FromName="Chénée" ToName="Loncin" DelaySeverity="3">
      <Delay hours="0" minutes="4" seconds="12" totalSeconds="252" />
      <OptimalTT hours="0" minutes="6" seconds="32" totalSeconds="392" />
      <ActualTT hours="0" minutes="10" seconds="44" totalSeconds="644" />
    </TravelTime>
    <TravelTime Id="1340752" Name="A602_LoncinChenee" FromName="Liège" ToName="Chénée" DelaySeverity="3">
      <Delay hours="0" minutes="2" seconds="32" totalSeconds="152" />
      <OptimalTT hours="0" minutes="5" seconds="3" totalSeconds="303" />
      <ActualTT hours="0" minutes="7" seconds="35" totalSeconds="455" />
    </TravelTime>
    <TravelTime Id="763062" Name="AlleurBiersetAéro" FromName="Alleur" ToName="Aéroport Bierset" DelaySeverity="1">
      <Delay hours="0" minutes="0" seconds="23" totalSeconds="23" />
      <OptimalTT hours="0" minutes="3" seconds="43" totalSeconds="223" />
      <ActualTT hours="0" minutes="4" seconds="6" totalSeconds="246" />
    </TravelTime>
    <TravelTime Id="1059491" Name="BergenGosseliesAéro" FromName="Mons" ToName="Gosselies Aéroport" DelaySeverity="4">
      <Delay hours="0" minutes="42" seconds="19" totalSeconds="2539" />
      <OptimalTT hours="0" minutes="22" seconds="0" totalSeconds="1320" />
      <ActualTT hours="1" minutes="4" seconds="19" totalSeconds="3859" />
    </TravelTime>
    <TravelTime Id="1002422" Name="ChénéeBiersetAéro" FromName="Chénée" ToName="Aéroport Bierset" DelaySeverity="3">
      <Delay hours="0" minutes="4" seconds="5" totalSeconds="245" />
      <OptimalTT hours="0" minutes="9" seconds="34" totalSeconds="574" />
      <ActualTT hours="0" minutes="13" seconds="39" totalSeconds="819" />
    </TravelTime>
    <TravelTime Id="1290014" Name="E19_AntwerpenMachelen" FromName="Anvers" ToName="Zaventem" DelaySeverity="4">
      <Delay hours="0" minutes="28" seconds="4" totalSeconds="1684" />
      <OptimalTT hours="0" minutes="18" seconds="5" totalSeconds="1085" />
      <ActualTT hours="0" minutes="46" seconds="9" totalSeconds="2769" />
    </TravelTime>
....

Part of the XSLT

Code:
  <xsl:template match="TravelTimeDocument" >
    <p>
      <table>
        <th>Name</th>
        <th>From</th>
        <th>To</th>
        <th>Reistijd in minuten</th>
        <th>Optimale reistijd in minuten</th>
        <th>Vertraging reistijd in minuten</th>
      <xsl:apply-templates />
      </table>
    </p>
  </xsl:template>


<xsl:template match ="TravelTimeDocument/TravelTimes/TravelTime[@Name='A602_CheneeLoncin']">
<tr > <xsl:if test="position() mod 2 =1"> <xsl:attribute name="bgcolor">#EEEEFF</xsl:attribute> </xsl:if>
<td>
<xsl:value-of select="@Name" />
</td> <td>
<xsl:value-of select="@FromName" />
</td>
<td>
<xsl:value-of select="@ToName" />
</td>
<td align="center">
<xsl:value-of select="round(ActualTT/@totalSeconds div 60)" />
</td>
<td align="center">
<xsl:value-of select="round(OptimalTT/@totalSeconds div 60)" />
</td>
<xsl:if test="@DelaySeverity=0"> <td bgcolor="#00FF00"><xsl:value-of select="Delay/@minutes"/></td> </xsl:if> <xsl:if test="@DelaySeverity=1"> <td bgcolor="#00FF00"><xsl:value-of select="Delay/@minutes"/></td> </xsl:if> <xsl:if test="@DelaySeverity=2"> <td bgcolor="#FFFF00"><xsl:value-of select="Delay/@minutes"/></td> </xsl:if> <xsl:if test="@DelaySeverity=3"> <td bgcolor="#FF9900"><xsl:value-of select="Delay/@minutes"/></td> </xsl:if> <xsl:if test="@DelaySeverity=4"> <td bgcolor="#FF0000"><xsl:value-of select="Delay/@minutes"/></td> </xsl:if> </tr>
</xsl:template>
 
Old October 28th, 2009, 01:32 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:template match="TravelTimes">
  <xsl:value-of select="sum(TravelTime[position() &lt; 3]/Delay/@hours)"/>
</xsl:template>
would add the 'hours' attribute values of the 'Delay' child elements of the first and second 'TravelTime' element.

If that is not what you want to achieve then consider to provide a sample of the output you want to create for the input sample you have posted.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 29th, 2009, 05:32 AM
Registered User
 
Join Date: Oct 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin

thanks for your response.
Look at the following page :http://rtbf.tivie.myroute.be/Flandre.aspx

You will see that the first 4 nodes (don't know if that is the correct name) have an underscore - in the name field - followed by number. In this example I display those 4 nodes, but actually I only need two of them; one containing the sum of A12_AnversStrombeek_1 + A12_AnversStrombeek_2 and another containing the sum of A12_StrombeekAnvers_1 + A12_StrombeekAnvers_2.

Hope this is a little clear.
 
Old October 29th, 2009, 07:36 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well that document you pointed me to is not XML and I don't see how it relates to the XML you posted earlier.
If you want the sum of two XML elements named A12_AnversStrombeek_1 and A12_AnversStrombeek_2 then why don't you simply do
Code:
sum(A12_AnversStrombeek_1 | A12_AnversStrombeek_2)
or for two items even
Code:
A12_AnversStrombeek_1 + A12_AnversStrombeek_2
?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 29th, 2009, 08:19 AM
Registered User
 
Join Date: Oct 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin. As it was an ASPX page you could not see the files...

Imagine in my XSLT I am at the 2 node (traveTime ID114135), and want to add the totalSeconds of the previous node (travelTime ID1307543). How would the syntax be ?

thanks


Below is the XM...

Code:
  <TravelTimes>
    <TravelTime Id="1307543" Name="A12_AnversStrombeek_1" FromName="Anvers" ToName="Bruxelles" DelaySeverity="1">
      <Delay hours="0" minutes="3" seconds="24" totalSeconds="204" />
      <OptimalTT hours="0" minutes="29" seconds="55" totalSeconds="1495" />
      <ActualTT hours="0" minutes="22" seconds="19" totalSeconds="1699" />
    </TravelTime>
    <TravelTime Id="114135" Name="A12_AnversStrombeek_2" FromName="Bruxelles" ToName="Strombeek Bever A12 / R0" DelaySeverity="0">
      <Delay hours="0" minutes="0" seconds="0" totalSeconds="0" />
      <OptimalTT hours="0" minutes="1" seconds="29" totalSeconds="89" />
      <ActualTT hours="0" minutes="1" seconds="29" totalSeconds="89" />
    </TravelTime>
    <TravelTime Id="1313527" Name="A12_StrombeekAnvers_1" FromName="Strombeek Bever A12 / R0" ToName="Bruxelles" DelaySeverity="0">
      <Delay hours="0" minutes="0" seconds="0" totalSeconds="0" />
      <OptimalTT hours="0" minutes="1" seconds="54" totalSeconds="114" />
      <ActualTT hours="0" minutes="1" seconds="54" totalSeconds="114" />
    </TravelTime>
    <TravelTime Id="114133" Name="A12_StrombeekAnvers_2" FromName="Bruxelles" ToName="Anvers" DelaySeverity="1">
      <Delay hours="0" minutes="5" seconds="25" totalSeconds="325" />
      <OptimalTT hours="0" minutes="28" seconds="0" totalSeconds="1680" />
      <ActualTT hours="0" minutes="33" seconds="26" totalSeconds="2006" />
    </TravelTime>
    <TravelTime Id="113711" Name="E19_AnversMeer" FromName="Bruxelles" ToName="E19 - A1 uitrit 1" DelaySeverity="2">
      <Delay hours="0" minutes="10" seconds="59" totalSeconds="659" />
      <OptimalTT hours="0" minutes="50" seconds="41" totalSeconds="3041" />
      <ActualTT hours="1" minutes="1" seconds="40" totalSeconds="3700" />
    </TravelTime>

Last edited by elgwiedo; October 29th, 2009 at 08:22 AM..
 
Old October 29th, 2009, 08:30 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

preceding-sibling::TravelTime[1] gives you the preceding sibling TravelTime element. Those elements however do not have a totalSeconds attribute so again it is not clear what you want to achieve.
preceding-sibling::TravelTime[1]/Delay/@totalSeconds might be what you are looking for if you want to access the 'totalSeconds' attribute of the 'Delay' child element of the preceding-sibling 'TravelTime' element.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 29th, 2009, 08:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't see for the life of my why you are finding this so difficult. There's clearly something you haven't grasped but I'm having trouble seeing what.

Code:
Delay/@totalSeconds + preceding-sibling::TravelTime/Delay/@totalSeconds
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding and Sorting nodes georgemeng XSLT 4 December 1st, 2008 12:13 PM
Compare values in different nodes fann XSLT 9 September 11th, 2008 06:25 AM
Adding nodes to JTree sriharshareddyk Java Basics 0 May 4th, 2007 01:18 PM
adding prefix to nodes and subnodes to output xml raghurns XSLT 9 November 17th, 2006 04:41 PM
Adding nodes to grandparent of current spencer.clark XSLT 2 August 12th, 2005 09:29 AM





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