tags:

views:

71

answers:

3

Hi All,

I have an xml structure:

<Date>Mon, 11 Aug 2009 13:15:10 GMT</Date> 

i want to extract only 15:10 or '15' and '10'. What is the best way to do that using xslt

+2  A: 

Try below; assumes the date is always in the same format.

<xsl:variable name='datetime' select="Date"/>

<xsl:variable name='time'  select='substring( $datetime, 17 , 5 )' />
Simmo
this will not work, if i change the month name
Wondering
The delimit by the spaces: <xsl:variable name="time" select="substring-before(substring-after( substring-after(substring-after(substring-after($datetime, ' '), ' '), ' '), ' '), ' ')" />
Simmo
@Wondering: If changing the month name breaks the pattern, it shows that your format is not a good one.
Alejandro
+3  A: 

If the date contains a colon only in the time field and time is always represented in the format HH:MM:SS then selecting a fixed length substring after first colon should solve your problem

untested XSLT one-liner

<xsl:variable name="time" select="substring(substring-after(Date, ':'), 1, 5)"/>
jasso
@jasso: Yes, this would work. +1
Alejandro
+2  A: 

Here is a general XSLT solution which would be useful also in converting a string containing a date in the format specified in the problem, into an XML-structured date:

This uses the FXSL 1.x template str-split-to-words to perform tokenization with more than one possible delimiters.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"&gt;

   <xsl:import href="strSplit-to-Words.xsl"/>

   <xsl:output indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">

      <xsl:variable name="vwordNodes">
        <xsl:call-template name="str-split-to-words">
          <xsl:with-param name="pStr" select="/"/>
          <xsl:with-param name="pDelimiters" select="' ,'"/>
        </xsl:call-template>
      </xsl:variable>

      <xsl:variable name="vTokens" select="ext:node-set($vwordNodes)/*"/>

      <xsl:variable name="vrtfDateTimeStruct">
          <date>
            <week-day-name val="{$vTokens[1]}"/>
            <day val="{$vTokens[2]}"/>
            <month-name val="{$vTokens[3]}"/>
            <year val="{$vTokens[4]}"/>
            <time val="{$vTokens[5]}"/>
            <zone val="{$vTokens[6]}"/>
          </date>
      </xsl:variable>

      <xsl:value-of select=
       "substring(ext:node-set($vrtfDateTimeStruct)/*/time/@val,1,5)"/>
    </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<Date>Mon, 11 Aug 2009 13:15:10 GMT</Date>

the wanted result is produced:

13:15

Do note: The string is converted completely into an XML-structured date using this code fragment:

      <xsl:variable name="vrtfDateTimeStruct">
          <date>
            <week-day-name val="{$vTokens[1]}"/>
            <day val="{$vTokens[2]}"/>
            <month-name val="{$vTokens[3]}"/>
            <year val="{$vTokens[4]}"/>
            <time val="{$vTokens[5]}"/>
            <zone val="{$vTokens[6]}"/>
          </date>
      </xsl:variable>
Dimitre Novatchev
can u pls explain , how does it work
Wondering
@Wondering, @Alejandro: I have completely replaced the solution with an XSLT one, that fully reconstructs the DateTime structure.
Dimitre Novatchev
@Dimitre: +1 beautiful answer. I think that the RTF should have the values as text nodes better than attribute values, so `string($vrtfDateTimeStruct)` could reconstruct the tokenized string...
Alejandro
@Alejandro: You are completely right. When you see me using attributes this means that I'm lazy and dont want to type longer... :)
Dimitre Novatchev