格式化字符串到数字时间(Formatting a string to digital time)

2019-11-04 04:26发布

<?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/3AM,为数字时代,例如,(15:00)使用XSL和BASH我的输出文件,与输出端是一个XML文档。

这是我迄今为止的xsl:

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

这个结果是出现在所有的“开始”和“结束”时间NaN的价值。

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

Answer 1:

考虑制定一个命名模板来进行转换,那么你可以把它分开的开始和结束时间。 我提出的假设输入将只包含一个小时元件,而不是分钟(即是形式为“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>


Answer 2:

我建议你尝试沿着线:

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


文章来源: Formatting a string to digital time
标签: xml bash xslt