我正在从客户端的服务器中提取自定义 XML 提要。我无法控制这种 XML 格式。为了重组 XML 结构,我使用 XSLT 通过运行wget和xsltproc在 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>