SunnyR8394
SunnyR8394

Reputation: 3

XSLT - Filter XML nodes with dates and current date ()

For below XML i need to delete all XML nodes of only Unit 6000 when field StartDate is not equal to any of the StartDate items of unit 5000 and (also make sure startDate is less than equal to current date and EndDate is greater than or equal to current date)

I tried to match a template but does to seems to be a good match

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

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

Upvotes: 0

Views: 749

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117073

First thing: in order to compare dates to current date you need XSLT 2.0 (or an alternative way to get the current date in XSLT 1.0, e.g. an extension function or a parameter).

Next, your input contains dateTimes, not dates. To make a valid comparison, you need to either compare them to the current dateTime or convert them to dates before comparing them to the current date. Try:

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>

Upvotes: 2

Related Questions