使用 XSLT 过滤顶部 XML 元素部分以外的内容

Filter other than top XML element section using XSLT

我的负载低于 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>

简单地过滤掉first之后的所有节点。由于您调用身份转换,只需直接在需要的节点上匹配,job_information.

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

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

Online Demo