使用 XSLT 筛选顶部 XML 元素部分以外的其他部分



>我有以下xml有效负载。我只需要最上面的job_information部分。要过滤掉的其他job_information部分。有没有办法实现这一点?

XML 有效负载:

<?xml version='1.0' encoding='UTF-8'?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>858</id>
<person>
<person_id_external>484304</person_id_external>
<employment_information>
<user_id>484304</user_id>
<job_information>
<end_date>2020-06-30</end_date>
<start_date>2020-06-23</start_date>
</job_information>
<job_information>
<end_date>2020-06-22</end_date>
<start_date>2020-05-11</start_date>
</job_information>
<job_information>
<end_date>2020-05-10</end_date>
<start_date>2020-01-01</start_date>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>

预期产出:

<?xml version='1.0' encoding='UTF-8'?>
<queryCompoundEmployeeResponse>
<CompoundEmployee>
<id>858</id>
<person>
<person_id_external>484304</person_id_external>
<employment_information>
<user_id>484304</user_id>
<job_information>
<end_date>2020-06-30</end_date>
<start_date>2020-06-23</start_date>
</job_information>
</employment_information>
</person>
</CompoundEmployee>
</queryCompoundEmployeeResponse>

我在XSL脚本下面尝试过,但不起作用:

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

<xsl:template match="//CompoundEmployee/person/employment_information/job_information[position()=1]"/>


</xsl:stylesheet>

只需在第一个节点之后的位置过滤掉所有节点即可。由于您调用了身份转换,因此只需直接在所需的节点上进行匹配,job_information.

调用空模板会删除节点及其内容,因为您不会对匹配项应用任何样式规则。由于大于符号是一个特殊符号,因此>在 XML 中使用实体&gt;

<xsl:template match="job_information[position() &gt; 1]" />

在线演示

最新更新