使用 xslt 使用 end_date 字段值识别最新节点

Identifying the latest node using end_date field values using xslt

我有一个需求,根据end_date值,从子节点中识别出最新记录。根据源 XML 数据,end_date 可能缺失(不存在)或将具有值。我需要根据这个顺序/condition

来挑选记录

a) 如果缺少 end_date 字段(任何一个节点),则应该选择那些记录。 b) 如果 end_date 已填充(对于所有节点)则需要选择最高的(最新日期)

尝试使用以下代码,但它仅在 end_date 存在时有效,并跳过那些缺少没有 end_Date.

的记录的代码

<xsl:for-each select="hm:job_information">
  <xsl:sort select="hm:end_date" data-type="text" order="descending" />
  <xsl:if test="position()=1">
    <xsl:text>"business_unit":"</xsl:text> <xsl:value-of select="./hm:business_unit" />
    <xsl:text>"company":"</xsl:text> <xsl:value-of select="./hm:company" />
  </xsl:if>
 </xsl:for-each>

下面是示例输入文件。我想用 80004122(如 business_unit)记录,因为该记录没有任何结束日期。如果假设两条记录 (80004122,70001634) 都有结束日期,则需要选择其中有最晚结束日期的记录。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
  <queryResponse xmlns="urn:sfobject.sfapi.successfactors.com" xmlns:ns2="urn:fault.sfapi.successfactors.com">
     <result>
        <sfobject>
           <id>18348</id>
           <type>CompoundEmployee</type>
           <person>
            <user_id>28009483</user_id>
                 <personal_information>
                     <first_name>Rodrigo</first_name>
                     <first_name_alt1>Rodrigo</first_name_alt1>
                     <formal_name>Rodrigo Lacey</formal_name>
                     <gender>M</gender>
                  </personal_information>   
                  <employment_information>
                      <job_information>
                        <business_unit>100</business_unit>
                        <company>11</company>
                        <start_date>2010-09-01</start_date>
                        <end_date>2013-08-01</end_date>
                      </job_information>
                      <job_information>
                        <business_unit>200</business_unit>
                        <company>22</company>
                        <start_date>2020-03-11</start_date>
                        <end_date>2021-08-01</end_date>
                      </job_information>
                      <job_information>
                        <business_unit>300</business_unit>
                        <company>33</company>
                        <start_date>2015-03-11</start_date>
                        <end_date>2016-10-01</end_date>
                      </job_information>
                </employment_information>
          </person>
           <execution_timestamp>2022-03-03T02:31:15.000Z</execution_timestamp>
           <version_id>2111P0</version_id>
        </sfobject>
        <numResults>1</numResults>
        <hasMore>false</hasMore>
        <querySessionId>c2172238-c6ed-4fd5-aa9a-6840a2fd1b26</querySessionId>
     </result>
  </queryResponse>
   </S:Body>
</S:Envelope>

考虑这个例子:

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"
exclude-result-prefixes="xs">

<xsl:template match="employment_information">
    <xsl:variable name="max-date" select="max(job_information/xs:date(end_date))" />
    <xsl:variable name="last-job" select="(job_information[not(end_date)], job_information[end_date=$max-date])[1]" />
    <xsl:value-of select="$last-job/business_unit"/>
</xsl:template>

</xsl:stylesheet>

已添加:

Can i do anything on the sort part ?

是的,您可以按如下方式排序:

<xsl:sort select="number(boolean(end_date))" data-type="number" order="ascending" />
<xsl:sort select="end_date" data-type="text" order="descending" />