对于下面的XML,当字段StartDate不等于单元5000的任何StartDate项时,我需要删除单元6000的所有XML节点,并且(还确保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 <= current-date())] [(EndDate >= current-date())]"/>
</xsl:stylesheet>
第一件事:为了比较日期和当前日期,您需要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>