【问题标题】:XSLT - Filter XML nodes with dates and current date ()XSLT - 过滤带有日期和当前日期的 XML 节点 ()
【发布时间】:2021-03-10 16:21:44
【问题描述】:

对于下面的 XML,当字段 StartDate 不等于单元 5000 的任何 StartDate 项并且(还要确保 startDate 小于等于当前日期并且 EndDate 大于或等于当前日期)

我试图匹配一个模板,但似乎是一个很好的匹配

<?xml version="1.0" encoding="UTF-8"?>
<AllRecords>
   <Records>
      <Record>
         <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST1</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2020-12-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST2</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2020-05-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2021-02-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>1000</company>
                  <Newplant>TEST3</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-02-01T00:00:00.000</StartDate>
            <Unit>5000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2020-12-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2000</company>
                  <Newplant>TEST4</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2020-01-01T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2021-03-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2010</company>
                  <Newplant>TEST5</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-01T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>A</Status>
         </Items>
         <Items>
            <EndDate>2021-06-31T00:00:00.000</EndDate>
            <PlantDetails>
               <PlantDetailsUS>
                  <company>2020</company>
                  <Newplant>TEST5</Newplant>
               </PlantDetailsUS>
            </PlantDetails>
            <StartDate>2021-01-093T00:00:00.000</StartDate>
            <Unit>6000</Unit>
            <Status>A</Status>
         </Items>
      </Record>
   </Records>
</AllRecords>

XSLT 代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:output method="xml" indent="yes"/>  
  <xsl:strip-space elements="*"/>  
  <xsl:template match="@*|node()"> 
    <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
  </xsl:template>  
  <xsl:template 
       match="Items[Unit=6000][not(StartDate = ../Items[Unit=5000]/StartDate)] [(StartDate &lt;= current-date())] [(EndDate &gt;= current-date())]"/> 
</xsl:stylesheet>

【问题讨论】:

  • “我尝试匹配模板” 发布您的尝试以便我们修复它,而不必从头开始为您编写代码。
  • 请不要在 cmets 中发布代码 - 而是编辑您的问题。
  • 已添加到问题中
  • 现在它是可见的。
  • 请注意2021-01-093T00:00:00.000 不是有效的日期时间。

标签: xslt xslt-1.0 xslt-2.0


【解决方案1】:

第一件事:为了将日期与当前日期进行比较,您需要 XSLT 2.0(或在 XSLT 1.0 中获取当前日期的替代方法,例如扩展函数或参数)。

接下来,您的输入包含 dateTimes,而不是 dates。要进行有效比较,您需要将它们与当前 dateTime 进行比较或将它们转换为日期,然后再将它们与当前日期进行比较。试试:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="k1" match="Items[Unit=5000]" use="StartDate" />

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Items[Unit=6000][not(key('k1', StartDate)) or xs:date(substring(StartDate, 1, 10)) gt current-date() or xs:date(substring(EndDate, 1, 10)) lt current-date()]"/>

</xsl:stylesheet>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-05-17
  • 2017-10-05
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多