将字符串格式化为数字时间

时间:2016-04-01 15:13:53

标签: xml bash xslt

<?xml version="1.0" encoding="UTF-8"?>
<clientlist>

  <client>
    <data key="id" value="111" />
    <data key="name" value="The Parlotones" />
    <data key="genre" value="Rock / Alternative" />
    <data key="description" value="The Parlotones are known for their electric, polished stage performances delivered against the backdrop of their deftly crafted and darkly romantic lyrics." />
    <data key="performanceday" value="Sunday" />
    <data key="performancetime" value="01PM-03PM" />
    <data key="picture" value="the-parlotones.jpg" />
  </client>

  <client>
    <data key="id" value="222" />
    <data key="name" value="ShortStraw" />
    <data key="genre" value="Folk / Acoustic" />
    <data key="description" value="Shortstraw are a joburg based band making waves on the national indie music scene in a big way." />
    <data key="performanceday" value="Sunday" />
    <data key="performancetime" value="03PM-05PM" />
    <data key="picture" value="shortstraw.jpg" />
  </client>

  <client>
    <data key="id" value="333" />
    <data key="name" value="Gangs of Ballet" />
    <data key="genre" value="Dance / Club" />
    <data key="description" value="Their music, which combines their fresh energy with their musically intriguing melodies and arrangements, has a hauntingly anthemic sound." />
    <data key="performanceday" value="Saturday" />
    <data key="performancetime" value="11AM-01PM" />
    <data key="picture" value="gangs-of-ballet.jpg" />
  </client>

</clientlist>

我想帮助将源文件中的3 PM / 3M时间转换为数字时间,例如使用XSL和BASH输出文件中的(15:00),输出为XML文档。

这是我到目前为止在xsl:

中所拥有的
<xsl:if test="not(PM)">
    <starts>
        <xsl:value-of select="number(substring before(data[@key='performancetime']/@value, '-'))" ></xsl:value-of>
    </starts>

此结果是NaN值出现在所有&#34;开始&#34;和&#34;结束&#34;次。

我真的需要一些帮助,谢谢:)

2 个答案:

答案 0 :(得分:0)

考虑制作一个命名模板来进行转换,然后您可以分别为开始和结束时间调用它。我假设输入只包含一个小时元素,而不是分钟(即“hhAM”或“hhPM”形式)

<xsl:template name="convertTime">
    <xsl:param name="time" />
    <xsl:choose>
        <xsl:when test="contains($time, 'PM')"><xsl:value-of select="number(substring($time, 1, 2)) + 12" /></xsl:when>
        <xsl:otherwise><xsl:value-of select="substring($time, 1, 2)" /></xsl:otherwise>
    </xsl:choose>
    <xsl:text>:00</xsl:text>
</xsl:template>

然后你可以在开始时间这样调用它,例如

<xsl:call-template name="convertTime">
   <xsl:with-param name="time" select="substring-before(data[@key = 'performancetime']/@value, '-')" />
</xsl:call-template>

试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="client">
      <xsl:variable name="allTimes" select="data[@key = 'performancetime']/@value" />
      <times>
      <start>
          <xsl:call-template name="convertTime">
              <xsl:with-param name="time" select="substring-before($allTimes, '-')" />
          </xsl:call-template>
      </start>
      <end>
          <xsl:call-template name="convertTime">
              <xsl:with-param name="time" select="substring-after($allTimes, '-')" />
          </xsl:call-template>
      </end>
      </times>
    </xsl:template>

    <xsl:template name="convertTime">
        <xsl:param name="time" />
        <xsl:choose>
            <xsl:when test="contains($time, 'PM')"><xsl:value-of select="number(substring($time, 1, 2)) + 12" /></xsl:when>
            <xsl:otherwise><xsl:value-of select="substring($time, 1, 2)" /></xsl:otherwise>
        </xsl:choose>
        <xsl:text>:00</xsl:text>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

我建议你按照以下方式尝试:

<xsl:template match="client">
    ...
    <xsl:variable name="times" select="data[@key='performancetime']/@value"/>
    <starts>
        <xsl:call-template name="time24">
            <xsl:with-param name="time" select="substring-before($times, '-')" />
          </xsl:call-template>
      </starts>
      <ends>
          <xsl:call-template name="time24">
              <xsl:with-param name="time" select="substring-after($times, '-')" />
          </xsl:call-template>
    </ends>
    ...
</xsl:template>  

<xsl:template name="time24">
    <xsl:param name="time" />
    <xsl:variable name="h12" select="substring($time, 1, 2)"/>
    <xsl:variable name="pm" select="contains($time,'P')"/>
    <xsl:variable name="h24" select="$h12 mod 12 + 12*$pm"/>
    <xsl:value-of select="format-number($h24, '00')"/>  
    <xsl:text>:00</xsl:text>
</xsl:template>