我想过滤一个传入的xml文档,只保留那些在某个重复的第n个子节点上包含比今天晚的日期的记录。我尝试将标识模板与模板一起使用,以仅匹配匹配中具有谓词的那些记录,但如果可能的话,我似乎无法获得正确的谓词。
输入示例:
<?xml version="1.0" encoding="utf-8"?>
<Example>
<Header>
<ElementA/>
<ElementB/>
</Header>
<Records>
<Record>
<Person>
<ElementC>1</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
</Employer>
</Employers>
</Record>
<Record>
<Person>
<ElementC>2</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
<History>
<HistoryRecord>
<Period>
<Date>2017-08-01</Date>
</Period>
</HistoryRecord>
<HistoryRecord>
<Period>
<Date>2017-08-01</Date>
</Period>
<Period>
<Date>2018-10-01</Date>
</Period>
</HistoryRecord>
</History>
</Employer>
</Employers>
</Record>
<Record>
<Person>
<ElementC>3</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
<History>
<HistoryRecord>
<Period>
<Date>2017-11-01</Date>
</Period>
</HistoryRecord>
</History>
</Employer>
</Employers>
</Record>
<Record>
<Person>
<ElementC>4</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
<History>
<HistoryRecord>
<Period>
<Date>2018-11-01</Date>
</Period>
</HistoryRecord>
</History>
</Employer>
</Employers>
</Record>
</Records>
</Example>
通缉输出:
<?xml version="1.0" encoding="utf-8"?>
<Example>
<Header>
<ElementA/>
<ElementB/>
</Header>
<Records>
<Person>
<ElementC>2</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
<History>
<HistoryRecord>
<Period>
<Date>2017-08-01</Date>
</Period>
</HistoryRecord>
<HistoryRecord>
<Period>
<Date>2017-08-01</Date>
</Period>
<Period>
<Date>2018-10-01</Date>
</Period>
</HistoryRecord>
</History>
</Employer>
</Employers>
</Record>
<Record>
<Person>
<ElementC>4</ElementC>
</Person>
<Employers>
<Employer>
<Identification>
<ElementD/>
<ElementE/>
</Identification>
<History>
<HistoryRecord>
<Period>
<Date>2018-11-01</Date>
</Period>
</HistoryRecord>
</History>
</Employer>
</Employers>
</Record>
</Records>
</Example>
<!-- Get today's date somehow -->
<xsl:variable name="todaysDate" select="'2018-09-06'"/>
<!-- You need the identity template -->
<xsl:template match="node()|@*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>
<xsl:template match="Record">
<!-- Since your date is in the yyyy-mm-dd format, you can do a text compare. -->
<xsl:if test=".//Date[. > $todaysDate][1]">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:if>
</xsl:template>