views:

418

answers:

2

I am using xsl version 2.0

I have a time field that I need to add 1 to. I am getting a formatted number from the the below node:

    <xsl:value-of select="Master/End/Timecode"/>
    <xsl:text>

The data in "Master/End/Timecode" looks like 01:00:00:00 basically time-- hours :minutes :seconds :frames the frames are a count from oo to 23. How to add 1 to the frames and have the seconds and minuts increment up if frames=23.

Example 01:00:59:23 plus 1 would give a result of 01:01:00:00. Any ideas

A: 

This sounds like something you'd either want to write a custom XPath function for, or process outside of XSLT. There are ways you could make this happen but they get very complex, very quickly.

Jweede
+2  A: 

Just convert it to frames, increment, and then convert back:

  <xsl:analyze-string select="Master/End/Timecode" regex="^(\d+):(\d+):(\d+):(\d+)$">
    <xsl:matching-substring>

      <!-- Split into components -->
      <xsl:variable name="hours"
                    select="xs:integer(regex-group(1))"/>
      <xsl:variable name="minutes"
                    select="xs:integer(regex-group(2))"/>
      <xsl:variable name="seconds"
                    select="xs:integer(regex-group(3))"/>
      <xsl:variable name="frames"
                    select="xs:integer(regex-group(4))"/>

      <!-- Calculate total amount of frames -->
      <xsl:variable name="total-frames"
                    select="$hours * 60 * 60 * 24 +
                            $minutes * 60 * 24 +
                            $seconds * 24 +
                            $frames "/>

      <!-- Add 1 frame -->
      <xsl:variable name="remaining-frames"
                    select="$total-frames + 1"/>

      <!-- Calculate new component values -->
      <xsl:variable name="new-hours"
                    select="$remaining-frames idiv (60 * 60 * 24)"/>
      <xsl:variable name="remaining-frames"
                    select="$remaining-frames mod (60 * 60 * 24)"/>
      <xsl:variable name="new-minutes"
                    select="$remaining-frames idiv (60 * 24)"/>
      <xsl:variable name="remaining-frames"
                    select="$remaining-frames mod (60 * 24)"/>
      <xsl:variable name="new-seconds"
                    select="$remaining-frames idiv 24"/>
      <xsl:variable name="new-frames"
                    select="$remaining-frames mod 24"/>

      <!-- Recombine components -->
      <xsl:value-of select="$new-hours"/>
      <xsl:text>:</xsl:text>
      <xsl:value-of select="$new-minutes"/>
      <xsl:text>:</xsl:text>
      <xsl:value-of select="$new-seconds"/>
      <xsl:text>:</xsl:text>
      <xsl:value-of select="$new-frames"/>

    </xsl:matching-substring>
  </xsl:analyze-string>
Pavel Minaev
Thanks for looking at this
are the commands analyze-string and regex valid commands for the sablotron processor?
No, Sablotron is an XSLT 1.0 processor; `analyze-string` and friends are all parts of XSLT 2.0. That said, you specifically mentioned that you're using XSLT 2.0 in the question, so now I'm confused, since you cannot possibly do it with Sablotron.
Pavel Minaev
Thanks, I am new to this. Learning as I go. I will look for a xslt processor and try again. I appreciate your help.
The only decent XSLT 2.0 processor is Saxon, IMO (http://saxon.sourceforge.net/). There's also Altova, but it's slower and non-cross-platform.
Pavel Minaev