灵活的XSL日期/日期时间转换

问题描述:

我们使用Tibco BusinessWorks将XML文档传递给Tibco BusinessEvents进程。因为BusinessEvents没有DATE格式,只有DATETIME,我们必须在发送到BusinessEvents之前更改源XML文档中的日期,并将响应的DateTime值映射回简单的日期。灵活的XSL日期/日期时间转换

这既烦人又麻烦。

为了提高BusinessWorks的性能,我正在编写一个样式表来处理映射。这是我得到的。

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:inf="http:/the.company.namespace"> 

<xsl:template match="node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()" /> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="inf:PriorExpirationDate | inf:OrderDate | inf:SegmentEffectiveDate | 
        inf:SegmentExpirationDate | inf:CancelDate | inf:NewBusinessEffectiveDate | 
        inf:NewBusinessExpirationDate | inf:RenewalEffectiveDate | inf:RenewalExpirationDate | 
        inf:QuestionDate | inf:ViolationDate | inf:ConvictionDate | 
        inf:EffectiveDate | inf:RatingDate | inf:AdvanceDate | 
        inf:SIDRevisionDate | inf:DriverLicensedDate | 
        inf:ESignatureDate | inf:UploadDate | inf:CancelDate | 
        inf:CancelProcessedDate | inf:CancelEffectiveDate | inf:CreatedDate | 
        inf:QuoteCreationDate | inf:QuoteModifiedDate | inf:QuoteExpirationDate | 
        inf:RateStartDate | inf:RateEndDate | inf:ChangeEffectiveDate | inf:PostDate | 
        inf:EffectiveDate | inf:ExpirationDate | inf:BirthDate | 
        inf:InstallmentDueDate | inf:CommercialDriverLicenseDate "> 
    <xsl:element name="{name()}"> 
     <xsl:value-of 
      select="concat(format-date(text(),'[Y0001]-[M01]-[D01]'), 'T00:00:00')" /> 
    </xsl:element> 
</xsl:template> 

虽然功能性的,这并不理想。我宁愿不必枚举每个我需要转换的元素,我宁愿指定一个需要转换的类型。

(1) XSL是否提供此功能?

(2)另外,还有一些元素名称可能是一个位置的DATE和其他元素的DATETIME。如果我通过名称知道父项,是否有排除某些DATETIME元素的有效方法?

(3)最后,有没有人看到超出问题范围的增强空间?

一些上下文:原始映射是在BusinessWorks的编辑器中完成的,其中GUI生成它自己的映射文件,几百行系列的if/then/else语句。对于一个50k文件(我们的平均值),这个数量相当于每次转换的接近20ms的开销,这个Web服务在不到50ms的时间内完成了它的实际工作。这是必须改进的瓶颈。

只需使用

<xsl:template match="*[. castable as xs:date]"> 
<!-- Your code here --> 
</xsl:template> 
+0

所以非常简单,正是我需要的。谢谢! – JHarnach 2013-03-14 13:49:29

+0

@JHarnach,不客气。是的,XSLT既强大又优雅。观看XSLT 3.0可以想象得到的新功能。 – 2013-03-14 14:36:50