VBA中的XSLT日期比较



由于限制,我无法使用2.0版或任何软件包。我目前正在使用VBA调用这个XSL模板,然后过滤掉20120101(YYYYMMDD)之前的日期。

我想基本上过滤所有日期大于给定可变日期的

'<xsl:template match=""BankStatementItemDetail[//BankStatementItemDetail[number(concat(substring(TransactionDate, 1, 4 ),substring( TransactionDate, 6, 2 ),substring( TransactionDate, 9, 2 ))) <= "" year & month & day ""  ]]"">`

交易日期格式2012-06-22T00:00:00年月日格式20120101

以及xml结构的示例

<BankStatementItemDetail Key="e744f079-c684-4567-b844-5442e69a1827">
    <Id>https:///xxx/components/x/bank-statement-item/e744f079-c084-7567-b844-5b42569a1827</Id>
    <Version>1</Version>
    <EffectiveStatus>Active</EffectiveStatus>
    <EffectiveStatusDateTime>2012-06-23T07:34:45</EffectiveStatusDateTime>
    <SecurityDescriptor>
        <IsEditable>true</IsEditable>
    </SecurityDescriptor>
    <account Code="xxxx" ScopeCode="xxxx" Uid="a0f60cd9-25a0-4955-a55c-2dc73bb0dd51">
        <Id>https://test/components/administration/account/a0f40cd9-23a0-4955-a55c-2dc73bb0dd51</Id>
        <Description>Trial Account</Description>
    </account>
</BankStatementItemDetail>

这个简单的转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match=
 "BankStatementItemDetail
   [20120101000000
   >
    translate(EffectiveStatusDateTime, '-:T', '')
   ]"/>
</xsl:stylesheet>

应用于以下XML文档时(基于提供的一个,但有两个BankStatementItemDetail元素,第二个元素在2012-01-01之前):

<details>
    <BankStatementItemDetail Key="e744f079-c684-4567-b844-5442e69a1827">
        <Id>https:///xxx/components/x/bank-statement-item/e744f079-c084-7567-b844-5b42569a1827</Id>
        <Version>1</Version>
        <EffectiveStatus>Active</EffectiveStatus>
        <EffectiveStatusDateTime>2012-06-23T07:34:45</EffectiveStatusDateTime>
        <SecurityDescriptor>
            <IsEditable>true</IsEditable>
        </SecurityDescriptor>
        <account Code="xxxx" ScopeCode="xxxx" Uid="a0f60cd9-25a0-4955-a55c-2dc73bb0dd51">
            <Id>https://test/components/administration/account/a0f40cd9-23a0-4955-a55c-2dc73bb0dd51</Id>
            <Description>Trial Account</Description>
        </account>
    </BankStatementItemDetail>
    <BankStatementItemDetail Key="e744f079-c684-4567-b844-5442e69a1827">
        <Id>https:///xxx/components/x/bank-statement-item/e744f079-c084-7567-b844-5b42569a1827</Id>
        <Version>1</Version>
        <EffectiveStatus>Active</EffectiveStatus>
        <EffectiveStatusDateTime>2011-12-31T08:39:52</EffectiveStatusDateTime>
        <SecurityDescriptor>
            <IsEditable>true</IsEditable>
        </SecurityDescriptor>
        <account Code="xxxx" ScopeCode="xxxx" Uid="a0f60cd9-25a0-4955-a55c-2dc73bb0dd51">
            <Id>https://test/components/administration/account/a0f40cd9-23a0-4955-a55c-2dc73bb0dd51</Id>
            <Description>Trial Account</Description>
        </account>
    </BankStatementItemDetail>
</details>

生成所需的正确结果(2012-01-01之前的BankStatementItemDetail已被"删除"):

<details>
   <BankStatementItemDetail Key="e744f079-c684-4567-b844-5442e69a1827">
      <Id>https:///xxx/components/x/bank-statement-item/e744f079-c084-7567-b844-5b42569a1827</Id>
      <Version>1</Version>
      <EffectiveStatus>Active</EffectiveStatus>
      <EffectiveStatusDateTime>2012-06-23T07:34:45</EffectiveStatusDateTime>
      <SecurityDescriptor>
         <IsEditable>true</IsEditable>
      </SecurityDescriptor>
      <account Code="xxxx" ScopeCode="xxxx" Uid="a0f60cd9-25a0-4955-a55c-2dc73bb0dd51">
         <Id>https://test/components/administration/account/a0f40cd9-23a0-4955-a55c-2dc73bb0dd51</Id>
         <Description>Trial Account</Description>
      </account>
   </BankStatementItemDetail>
</details>

您可以使用substring()函数和隐式类型转换。例如,这个XSLT1.0样式表。。。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="ref-year" select="2012" />
<xsl:variable name="ref-mon"  select="6" />
<xsl:variable name="ref-day"  select="23" />
<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="BankStatementItemDetail[
      ((substring( EffectiveStatusDateTime, 1, 4) * 416) +
       (substring( EffectiveStatusDateTime, 6, 2) *  32) +
       (substring( EffectiveStatusDateTime, 9, 2)      )) >=
      (($ref-year * 416) + ($ref-mon * 32) + $ref-day)
    ]"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>

应用于此输入时。。。

<BankStatementItems>
  <BankStatementItemDetail id="1">
    <EffectiveStatusDateTime>2012-06-23T07:34:45</EffectiveStatusDateTime>
  </BankStatementItemDetail>
  <BankStatementItemDetail id="2">
    <EffectiveStatusDateTime>2011-09-20</EffectiveStatusDateTime>
  </BankStatementItemDetail>
</BankStatementItems> 

收益率。。。

<BankStatementItems>
  <BankStatementItemDetail id="1">
    <EffectiveStatusDateTime>2012-06-23T07:34:45</EffectiveStatusDateTime>
  </BankStatementItemDetail>
</BankStatementItems> 

相关内容

  • 没有找到相关文章

最新更新