使用 XSLT 使用从同一输入 XML 中提取的最新日期更新输入 XML

Update an input XML with latest date extracted from same input XML using XSLT

Input XML is below. I need output xml with the latest date extracted from date field and update the date field with max date value.

<?xml version="1.0" encoding="UTF-8"?><rsp:response xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1">
<rsp:period>
    <res:Period>
        <rsp:date>2020-07-06T19:38:39</rsp:date>
    </res:Period>
</rsp:period>
<rsp:period>
    <res:Period>
        <rsp:date>2020-08-07T20:38:39</rsp:date>
    </res:Period>
</rsp:period>
<rsp:period>
    <res:Period>
        <rsp:date>2020-05-06T19:18:39</rsp:date>
    </res:Period>
</rsp:period></rsp:response>

Below is the XLST being used for input xml

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rsp="rsp.com/employee/Response/v30">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

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

<xsl:template
    match="*[local-name() = 'response']/*[local-name() = 'period']/*[local-name() = 'Period']/*[local-name() = 'date']">
    <xsl:copy>
        <xsl:apply-templates/>
        <xsl:copy-of select="*[local-name() = 'date']"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/">
    <xsl:for-each
        select="*[local-name() = 'response']/*[local-name() = 'period']/*[local-name() = 'Period']/*[local-name() = 'date']">

        <xsl:sort select="translate(., '-T:Z', '')" data-type="number"/>
        <xsl:choose>
            <xsl:when test="position() = last()">
                <xsl:copy-of select="."/>
            </xsl:when>
        </xsl:choose>

    </xsl:for-each>
</xsl:template></xsl:stylesheet>

Below is the output for this XSLT. It is able to extract the latest date but not able to update the latest extracted date in incoming date field values.

<rsp:date xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:rsp="rsp.com/employee/Response/v30"
      xmlns:res="res.com/Member/details/v1">2020-08-07T20:38:39</rsp:date>

Below is the expected output

<?xml version="1.0" encoding="UTF-8"?><rsp:response xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1">
<rsp:period>
    <res:Period>
        <rsp:date>2020-08-07T20:38:39</rsp:date>
    </res:Period>
</rsp:period>
<rsp:period>
    <res:Period>
        <rsp:date>2020-08-07T20:38:39</rsp:date>
    </res:Period>
</rsp:period>
<rsp:period>
    <res:Period>
        <rsp:date>2020-08-07T20:38:39</rsp:date>
    </res:Period>
</rsp:period></rsp:response>

我建议你这样处理:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rsp="rsp.com/employee/Response/v30"
xmlns:res="res.com/Member/details/v1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="max-date">
    <xsl:for-each select="/rsp:response/rsp:period/res:Period">
        <xsl:sort select="rsp:date"/>
        <xsl:choose>
            <xsl:when test="position() = last()">
                <xsl:value-of select="rsp:date"/>
            </xsl:when>
        </xsl:choose>
    </xsl:for-each>
</xsl:variable>

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

<xsl:template match="rsp:date">
    <xsl:copy>
        <xsl:value-of select="$max-date"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

注:

  1. 没有必要使用像 *[local-name()='response'] 这样的 hack(正如你在 上被告知的那样);

  2. ISO 8601 日期将按原样作为文本正确排序。