2014-04-30 30 views
0

我有一个xml文件,这个充满节点:提取时间1.0

<FlightSegment DepartureDateTime="2014-05-02T14:24:00" ArrivalDateTime="2014-05-02T15:42:00"/> 
<FlightSegment DepartureDateTime="2014-05-02T16:24:00" ArrivalDateTime="2014-05-02T17:42:00"/> 

,我需要提取@DepartureDateTime和@ArrivalDateTime的只是时间的HTML输出。到目前为止,我做了一个xsl:for-each和一个xsl:value-of返回完整的字符串,但我尝试使用fn:hours-from-dateTime,它不起作用。要么我做错了什么,要么只能在2.0下工作。 我怎样才能从这个字符串中获得时间?

这是我的代码:

<table> 
     <xsl:for-each select="FlightSegment"> 
      <tr> 
       <td> 
        Departure: 
       </td> 
       <td> 
        <fn:hours-from-dateTime(xsl:value-of select="@DepartureDateTime")/>:<fn:minutes-from-dateTime(xsl:value-of select="@DepartureDateTime")/>:<fn:seconds-from-dateTime(xsl:value-of select="@DepartureDateTime")/> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Arrival: 
       </td> 
       <td> 
        <fn:hours-from-dateTime(xsl:value-of select="@ArrivalDateTime")/>:<fn:minutes-from-dateTime(xsl:value-of select="@ArrivalDateTime")/>:<fn:seconds-from-dateTime(xsl:value-of select="@ArrivalDateTime")/> 
       </td> 
      </tr> 
     </xsl:for-each> 
</table> 

在此先感谢。

+1

如果你看到一个描述为'fn:anything'的函数,那么它几乎肯定是2.0。 XPath 1.0中默认可用的唯一函数是[theses](http://www.w3.org/TR/xpath/#corelib),本质上是从节点提取信息的函数('namespace-uri','local -name'等),一些基本的字符串操作(没有正则表达式!)和一些数字函数,如'sum'和'round'。而且他们不需要命名空间前缀 - XPath 1.0对于http:// www.w3.org/2005/xpath-functions'命名空间一无所知。 –

回答

3

从以下提取日期和时间:

<Root> 
<myDate>2014-05-01T07:24:00</myDate> 
</Root> 

我用下面的函数来获取数据,或转换成其他格式:

<p> 
Extract just the date: <br /> 
<xsl:value-of select="substring(/Root/myDate, 1, 10)" /> 
</p> 

<p> 
Extract just the time: <br /> 
<xsl:value-of select="substring(/Root/myDate, 12, 5)" /> 
</p> 

<p>Convert to Short date format dd-MM-yyyy: <br /> 
<xsl:value-of select="concat(substring(/Root/myDate, 9, 2), '-', substring(/Root/myDate, 6, 2), '-', substring(/Root/myDate, 1, 4))" /> 
</p> 

我有必要用日期字符串中的完整月份名称,因此请使用以下样式表进行查找。虽然我从来没有设法使得月份值的子字符串函数在查找xpath的[]中工作,但它从未对使用变量产生的结果的性能产生影响。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lookup="http://any.com/lookup" exclude-result-prefixes="lookup"> 
    <xsl:output method="html" indent="yes" encoding="UTF-8" /> 
    <xsl:decimal-format name="NN" NaN="-" /> 
    <lookup:Months> 
     <Month index="01" string="January" /> 
     <Month index="02" string="February" /> 
     <Month index="03" string="March" /> 
     <Month index="04" string="April" /> 
     <Month index="05" string="May" /> 
     <Month index="06" string="June" /> 
     <Month index="07" string="July" /> 
     <Month index="08" string="August" /> 
     <Month index="09" string="September" /> 
     <Month index="10" string="October" /> 
     <Month index="11" string="November" /> 
     <Month index="12" string="December" /> 
    </lookup:Months> 
    <xsl:template match="/"> 
     <html> 
      <body> 
       <p> 
        Convert to Long date format dd-MMM-yyyy: <br /> 
        <xsl:variable name="myMonth" select="substring(/Root/myDate, 6, 2)" /> 
        <xsl:value-of select="concat(substring(/Root/myDate, 9, 2), ' ', document('')/*/lookup:Months/Month[@index=$myMonth]/@string, ' ', substring(/Root/myDate, 1, 4))" /> 
       </p> 
      </body> 
     </html> 

    </xsl:template> 
</xsl:stylesheet> 
+0

它的工作原理。谢谢。现在我可以继续阅读其他代码了。 – RodrigoHernandez

0

你想要的东西,这会给你一个快速的结果:

<td> 
    <xsl:value-of select="substring-after(@DepartureDateTime, 'T')"/> 
</td> 

如果您放置在一个变量,你可以再使用substring函数提取时间的单个组件,如果您需要。

+0

“*我不确定它可以在所有XSLT 1.0处理器上工作。*”哪种XSLT 1.0处理器可以工作? –

+0

可能没有:)十年前,我为Java中的XSLT 1.0编写了一些模式感知的扩展。我用了他们几次,从未更多。 – helderdarocha

+0

好吧,XSLT 1.0不支持XML模式,并且它明确地这样说。附:我对你的编辑并不满意,留下我的评论在空中...... –