XSLT 映射转换 - 节点筛选



我在创建 xslt 条件以使节点过滤器到最新的作业信息时遇到问题。

以下 XML 输入

<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>11111</id>
<person>
<employment_information>
<job_information>
<end_date>9999-12-31</end_date>
<start_date>2017-05-17</start_date>
</job_information>
<job_information>
<end_date>2018-12-31</end_date>
<start_date>2017-05-17</start_date>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>

这是我想要的 xml 输出

<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>11111</id>
<person>
<employment_information>
<job_information>
<end_date>9999-12-31</end_date>
<start_date>2017-05-17</start_date>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>

这是 XSLT 输入

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsl xs">
<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>

<!--remove all employees with past and previous dalta to have only current data-->
<xsl:template match="//queryCompoundEmployeeResponse/CompoundEmployee/person/employment_information/job_information[((start_date &lt; current-date()) and (job_information/end_date &gt; current-date()))]"/>

</xsl:stylesheet>

规则很简单,最新的job_information节点始终位于xml的最顶部,我希望在整个xml输入中只有最新的job_information。

它与xpath过滤器一起工作:

/queryCompoundEmployeeResponse/CompoundEmployee[(person/employment_information/job_information[1])]

但我需要在 xslt 条件中具有上述 xpath 条件,以start_date和end_date。

你可以帮我吗?

您的模板应与过去结束日期的任何<job_information>匹配。

<xsl:template match="job_information[xs:date(end_date) &lt; current-date()]" />

我认为没有必要让它比这更复杂。

请注意,也不必使match表达式完全限定。以上将适用于任何<job_information>,无论它嵌套有多深。