2016-06-15 30 views
0

我想获得TimestampCreate与ControlPoint时间戳匹配的状态。 这场比赛不必毫秒匹配如何获得与ControlPoint时间戳匹配的TimestampCreate状态?

我不知道该怎么做。

我使用XSLT 1.0 预计输出

<ProtectionOrderStatus> 
    <ProtectionOrderStatusCode>SBJO</ProtectionOrderStatusCode> 
    <ProtectionOrderStatusDate>2016-05-09</ProtectionOrderStatusDate> 
</ProtectionOrderStatus> 

我的XML 1.0代码

<?xml version="1.0" encoding="UTF-8"?> 
<Integration> 
    <ControlPoint Timestamp="5/9/2016 2:34:34 PM" UserID="Kuku">SAVE</ControlPoint> 
    <ProtectionOrders> 
     <ProtectionOrder Op="E" InternalProtectionOrderID="11831"> 
      <Statuses> 
       <Status> 
        <Current>true</Current> 
        <Active>No</Active> 
        <Date>05/09/2016</Date> 
        <Type Word="DISMISSED">Dismissed</Type> 
        <TimestampCreate Op="A">05/09/2016 14:34:48:633</TimestampCreate> 
       </Status> 
       <Status Op="A"> 
        <Current>false</Current> 
        <Active>Yes</Active> 
        <Date Op="A">05/09/2016</Date> 
        <Type Op="A" Word="SBJO">Signed By Judicial Officer</Type> 
        <TimestampCreate>05/09/2016 14:34:34:737</TimestampCreate> 
       </Status> 
       <Status> 
        <Current>false</Current> 
        <Active>No</Active> 
        <Date>12/30/2014</Date> 
        <Type Word="DRAFT">Draft</Type> 
        <TimestampCreate>05/09/2016 14:34:14:987</TimestampCreate> 
       </Status> 
      </Statuses> 
     </ProtectionOrder> 
    </ProtectionOrders> 
</Integration> 

我的XSLT代码

<?xml version="1.0" encoding="UTF-8"?> 
    <ProtectionOrderStatus> 
    <ProtectionOrderStatusCode> 
     <xsl:value-of select="Statuses/Status/Type/@Word"/> 
    </ProtectionOrderStatusCode> 
    <ProtectionOrderStatusDate> 
     <xsl:value-of select="Statuses/Status/Date"/> 
    </ProtectionOrderStatusDate> 
</ProtectionOrderStatus> 

我的XSLT是产生下面的输出这是错误的

 <ProtectionOrderStatus> 
     <ProtectionOrderStatusCode>DISMISSED</ProtectionOrderStatusCode> 
     <ProtectionOrderStatusDate>2016-05-09</ProtectionOrderStatusDate> 
    </ProtectionOrderStatus> 
+0

您正在使用一些非标准的扩展函数:很难说如何在不知道使用哪个XSLT处理器的情况下使用它们。 - 另外,如果通过“匹配”你的意思是“匹配到最近的秒”,你应该明确地陈述。 –

回答

0

以下样式:

XSLT 1.0

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

<xsl:variable name="ctl" select="Integration/ControlPoint/@Timestamp"/> 
<xsl:variable name="date" select="substring-before($ctl, ' ')"/> 
<xsl:variable name="time" select="substring-before(substring-after($ctl, ' '), ' ')"/> 

<xsl:variable name="month" select="substring-before($date, '/')"/> 
<xsl:variable name="day" select="substring-before(substring-after($date, '/'), '/')"/> 
<xsl:variable name="year" select="substring-after(substring-after($date, '/'), '/')"/> 

<xsl:variable name="h12" select="substring-before($time, ':')"/> 
<xsl:variable name="pm" select="contains($ctl,'PM')"/> 
<xsl:variable name="h" select="$h12 mod 12 + 12 * $pm"/> 

<xsl:variable name="ts"> 
    <xsl:value-of select="format-number($month, '00')"/> 
    <xsl:text>/</xsl:text> 
    <xsl:value-of select="format-number($day, '00')"/> 
    <xsl:text>/</xsl:text> 
    <xsl:value-of select="$year"/> 
    <xsl:text> </xsl:text> 
    <xsl:value-of select="$h"/> 
    <xsl:text>:</xsl:text> 
    <xsl:value-of select="substring-after($time, ':')"/> 
</xsl:variable> 

<xsl:template match="/"> 
    <xsl:variable name="status" select="//Status[starts-with(TimestampCreate, $ts)]"/> 
    <ProtectionOrderStatus> 
     <ProtectionOrderStatusCode> 
      <xsl:value-of select="$status/Type/@Word"/> 
     </ProtectionOrderStatusCode> 
     <ProtectionOrderStatusDate>  
      <xsl:value-of select="$status/Date"/> 
     </ProtectionOrderStatusDate> 
    </ProtectionOrderStatus> 
</xsl:template> 

</xsl:stylesheet> 

应用到你的榜样输入,将返回:

<?xml version="1.0" encoding="UTF-8"?> 
<ProtectionOrderStatus> 
    <ProtectionOrderStatusCode>SBJO</ProtectionOrderStatusCode> 
    <ProtectionOrderStatusDate>05/09/2016</ProtectionOrderStatusDate> 
</ProtectionOrderStatus> 

注意

  • 这里假定TimestampCreate小时是零填充到两位数(您的例子是模糊的在这方面);

  • 如果多于一个Status匹配条件,则只返回第一个值。

+0

真棒迈克尔。使用你的解决方案,我的代码现在返回正确的输出。同一时间戳创建的状态不会超过一个。 – user3781064

+0

我有一个问题给你Michael.hor257k。我的代码在使用你的代码后正在工作。然而,我的老板要求我使用样式表中的内部函数。因为如果我编辑问题,我可能会失分,你会推荐做什么?我应该编辑问题还是应该发布一个新问题,我可以将样式表与我应该调用的函数一起转换TimestampCreate和TimeStamp?请指教。 – user3781064

+0

我建议你发布一个新问题 - 并确保我们了解这些功能是什么,他们究竟做了什么。虽然我怀疑你不能在你输入的日期使用它们。 –