根据下级中的日期删除节点

bvhaajcl  于 2022-10-07  发布在  其他
关注(0)|答案(1)|浏览(109)

使用SAP CPI作为工具并使用Groovy脚本,如果“Letter”节点的创建日期早于同一级别的其他节点,则需要删除该节点。

给定此XML:

<root>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>699</offerLetterId>
                <createdBy>00004221</createdBy>
                <createDate>2022-09-15T13:20:17.000</createDate>
            </Letter>
            <Letter>
                <offerLetterId>724</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-26T13:27:39.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>723</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-26T13:27:04.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>695</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-03T14:02:37.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
</root>

在第一个JobApplication节点中,它有两个“Letter”节点,我需要保留较新的一个:**“2022-09-26T13:27:39.000”**并删除另一个节点。

结果应该是这样的:

<root>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>724</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-26T13:27:39.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>723</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-26T13:27:04.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
    <JobApplication>
        <OfferLetter>
            <Letter>
                <offerLetterId>695</offerLetterId>
                <createdBy>00001472</createdBy>
                <createDate>2022-09-03T14:02:37.000</createDate>
            </Letter>
        </OfferLetter>
    </JobApplication>
</root>
lyfkaqu1

lyfkaqu11#

使用XSLT 3:

<xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="OfferLetter/Letter[not(xs:dateTime(createDate) = max(../Letter/createDate/xs:dateTime(.)))]"/>

在XSLT 2中,您需要删除xsl:mode声明并将其拼写为模板

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

相关问题