How to extract this format in xslt

2019-02-19 13:03发布

问题:

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

回答1:

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)"/>


回答2:

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 )' />


回答3:

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">

   <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>


标签: xml xslt xpath