Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:49:12+00:00 2026-06-01T14:49:12+00:00

I just learnt XSLT today when I wanted to pretty print the playlist export

  • 0

I just learnt XSLT today when I wanted to pretty print the playlist export (xml) from itunes. I finally understood how XSLT pertains to XML and how useful it can be. I picked up the code from
iTunes2Html and customized it for my own purposes. I have created a dynamic column which is the playtime/duration for a song (useful for people/djs who set start and end times!). What I wanted at the end of the display is a sumtotal of all the duration column. I can’t figure out how to do that. I’ve researched on stack overflow and google with no luck so far. The results I have found have been confusing.

So can anybody can help me figure out how to calculate the total of a dynamic/calculated column? see similar question

Here is the relevant part of the XSLT

<td> <!-- calculating duration : formattime(end | total - start | 0), is there a simpler way to do it? -->
<xsl:variable name="totduration">
 <xsl:choose>
    <xsl:when test="string(number(following-sibling::Start_Time))='NaN'">
        <xsl:value-of select="following-sibling::Stop_Time"/>
    </xsl:when>
    <xsl:when test="string(number(following-sibling::Stop_Time))='NaN'">
        <xsl:value-of  select="number(following-sibling::Total_Time) - number(following-sibling::Start_Time)"/>
    </xsl:when>          
    <xsl:otherwise>
        <xsl:value-of  select="number(following-sibling::Stop_Time) - number(following-sibling::Start_Time)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<!-- for the javascript approach using jquery. didn't work -->
<span class="sumData" style="display:none"> <xsl:value-of select="$totduration"/></span> 
<xsl:call-template name="formatTime">
  <xsl:with-param name="t" select="$totduration"/>
</xsl:call-template>
</td>

Edit: Clarifying the logic of the duration calculation. Since start and stop are optional, the actual formula is (stop | total) – (start | 0). This means that if neither exists, then duration is identical to length i.e. it plays from start to end! Every song has that property. However if even one of the nodes (start or stop) is defined then the duration can be different from the total length of the song.

some sample XML

  <songlist>
<song>
  <Track_ID>169</Track_ID>
  <Name>AAAAA</Name>
  <Album>AAAAA</Album>
  <Genre>AAAAA</Genre>
  <Start_Time>78000</Start_Time>
  <Stop_Time>122000</Stop_Time>
  <Total_Time>357537</Total_Time>
</song>
<song>
  <Track_ID>174</Track_ID>
  <Name>BBBBBB</Name>
  <Artist>BBBBBB</Artist>
  <Album>BBBBBB</Album>
  <Genre>BBBBBB</Genre>
  <Stop_Time>120000</Stop_Time>
  <Total_Time>275043</Total_Time>
</song>
<song>
  <Track_ID>177</Track_ID>
  <Name>CCCCCC</Name>
  <Artist>CCCCCC</Artist>
  <Album>CCCCCC</Album>
  <Genre>CCCCCC</Genre>
  <Stop_Time>62000</Stop_Time>
  <Total_Time>287738</Total_Time>
</song>
<song>
  <Track_ID>180</Track_ID>
  <Name>DDDDDDD</Name>
  <Artist>DDDDDDD</Artist>
  <Album>DDDDDDD</Album>
  <Genre>DDDDDDD</Genre>
  <Start_Time>43000</Start_Time>
  <Total_Time>400274</Total_Time>
</song>

This is the output
Screenshot

The Desired Output as it would appear at the bottom of the duration column (i.e. sum of all duration):

Total Playtime Duration - 9:43

Also is there a better way (that what is shown above) to do this formula in xslt? I am currently using xsl:choose to check for NaNs and use the alternative accordingly. I can’t help but think that there has to be a better way.

formattime((stop | total) - (start | 0))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:49:13+00:00Added an answer on June 1, 2026 at 2:49 pm

    This should give you exactly what you are looking for:

    <xsl:variable name="total" select="
      sum(song[Stop_Time &gt; 0]/Stop_Time | song[not(Stop_Time &gt; 0)]/Total_Time)
      -
      sum(song/Start_Time)
    " />
    

    For your XML sample this evaluates to 583274, i.e. 9m, 43s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently learnt Python. I liked it. I just wanted to use it for
I just learnt about how to parse data in Xcode using NSXMLPARSER. In order
Okay, just learnt LINQ syntax about ten minutes ago, made a first program: public
I am new to Java Persistence API . I have just learnt it and
First the disclaimer: I never learnt any programming in school, and just have to
I've never learnt JavaScript, but I imagine this is quite a simple problem. Just
I just learnt about Enums and Types in Ada and decided to write a
I just learnt that transactional tasks cannot be named. Why is that? It's as
I just learnt basic Servlets and JSP technology and am designing a simple website
I've begun playing with the android NDK. One of the things I've just learnt

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.