2

我正在从客户端的服务器中提取自定义 XML 提要。我无法控制这种 XML 格式。为了重组 XML 结构,我使用 XSLT 通过运行wgetxsltproc在 RHEL 5 下应用自定义样式表。在我们需要进行一些日期转换之前,一切正常。

即将到来的饲料是这样的......

...
<event>
   <date>October 3, 2013</date>
   <time>2:00 PM - 4:00 PM</time>
   <moar_xml_here/>
</event>
...

但是我需要一个独特的<startDateTime>and <endDateTime>

我目前正在转换模板中的日期部分,如下所示。我正在查看这段代码,想知道是否有更好的方法。

<xsl:template match="date">
    <!-- year first -->
    <xsl:value-of select="normalize-space(substring-after(., ','))"/>

    <!-- month  -->
    <xsl:choose>
        <xsl:when test="contains(., 'January')">
            <xsl:value-of select="-01"/>
        </xsl:when>
        <xsl:when test="contains(., 'February')">
            <xsl:value-of select="-02"/>
        </xsl:when>
        <xsl:when test="contains(., 'March')">
            <xsl:value-of select="-03"/>
        </xsl:when>
        <xsl:when test="contains(., 'April')">
            <xsl:value-of select="-04"/>
        </xsl:when>
        <xsl:when test="contains(., 'May')">
            <xsl:value-of select="-05"/>
        </xsl:when>
        <xsl:when test="contains(., 'June')">
            <xsl:value-of select="-06"/>
        </xsl:when>
        <xsl:when test="contains(., 'July')">
            <xsl:value-of select="-07"/>
        </xsl:when>
        <xsl:when test="contains(., 'August')">
            <xsl:value-of select="-08"/>
        </xsl:when>
        <xsl:when test="contains(., 'September')">
            <xsl:value-of select="-09"/>
        </xsl:when>
        <xsl:when test="contains(., 'October')">
            <xsl:value-of select="-10"/>
        </xsl:when>
        <xsl:when test="contains(., 'November')">
            <xsl:value-of select="-11"/>
        </xsl:when>
        <xsl:when test="contains(., 'December')">
            <xsl:value-of select="-12"/>
        </xsl:when>
    </xsl:choose>

    <!-- Get everything before the comma, find the position of the space, and return the remaining value (day as date) -->
    <xsl:variable name="working_to_day" select="normalize-space(substring-before(. , ','))"/>

    <xsl:variable name="space_pos" select="string-length(normalize-space(substring-before(. , ' ')))+1"/>

    <xsl:value-of select="string('-')"/>
    <xsl:value-of select="normalize-space(translate(substring($working_to_day, $space_pos), ' ', '0'))"/>
</xsl:template>
4

1 回答 1

2

我怀疑你想要两位数的值,但请注意

<xsl:value-of select="-08"/>

<xsl:value-of select="-8"/>

并使-8

我可能会使用 substring-before 如下所示:

$ saxon date.xml date.xsl

original:
 -1

new:
  01


original:
 -10

new:
  10

<x>

<date>January</date>

<date>October</date>

</x>

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="date">
original:
 <xsl:choose>
  <xsl:when test="contains(., 'January')">
   <xsl:value-of select="-01"/>
  </xsl:when>
  <xsl:when test="contains(., 'February')">
   <xsl:value-of select="-02"/>
  </xsl:when>
  <xsl:when test="contains(., 'March')">
   <xsl:value-of select="-03"/>
  </xsl:when>
  <xsl:when test="contains(., 'April')">
   <xsl:value-of select="-04"/>
  </xsl:when>
  <xsl:when test="contains(., 'May')">
   <xsl:value-of select="-05"/>
  </xsl:when>
  <xsl:when test="contains(., 'June')">
   <xsl:value-of select="-06"/>
  </xsl:when>
  <xsl:when test="contains(., 'July')">
   <xsl:value-of select="-07"/>
  </xsl:when>
  <xsl:when test="contains(., 'August')">
   <xsl:value-of select="-08"/>
  </xsl:when>
  <xsl:when test="contains(., 'September')">
   <xsl:value-of select="-09"/>
  </xsl:when>
  <xsl:when test="contains(., 'October')">
   <xsl:value-of select="-10"/>
  </xsl:when>
  <xsl:when test="contains(., 'November')">
   <xsl:value-of select="-11"/>
  </xsl:when>
  <xsl:when test="contains(., 'December')">
   <xsl:value-of select="-12"/>
  </xsl:when>
 </xsl:choose>

新的:

  <xsl:value-of select="format-number(
                        string-length(substring-before(
     'JanFebMarAprMayJunJulAugSepOctNovDec',
         substring(.,1,3))) div 3 + 1,'00')"/>
</xsl:template>

</xsl:stylesheet>
于 2013-11-05T23:10:06.073 回答