0

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>

1 Answer 1

0

I would suggest you approach it this way:

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>

Note:

  1. There is no need to use a hack like *[local-name()='response'] (as you were already told on your previous question);

  2. ISO 8601 dates will sort correctly as text as they are.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.