VBA中的XSLT日期比较

时间:2012-09-11 01:44:49

标签: vba xslt xslt-1.0

由于限制,我无法使用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 ""  ]]"">`

TransactionDate格式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>

2 个答案:

答案 0 :(得分:1)

这个简单的转型

<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>

答案 1 :(得分:0)

您可以使用substring()函数和隐式类型转换。例如,这个XSLT 1.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>