0

I would like to sort the EventTime from the sample xml document I have pasted. I want to display the most recent EventTime.

My sample xml document

<Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="IXML Case Notification Test" MessageID="67085056" xmlns="">
    <Case InternalID="1617090499" ID="12125626" xmlns:user="http://tylertechnologies.com">
        <CaseEvent Date="05/22/2015" ID="160828152" InternalEventID="1721879467" xmlns:reslib="urn:reslib">
            <EventTime>8:49 AM</EventTime>
        </CaseEvent>
        <CaseEvent Date="05/28/2015" ID="160828600" InternalEventID="1721879818" xmlns:reslib="urn:reslib">
            <EventTime>1:39 PM</EventTime>
        </CaseEvent>
    </Case> 
</Integration>

xsl line of code to sort which is not working

<xsl:sort select="substring-after-last(EventTime)" order="descending"/>
Angela
  • 69
  • 5

1 Answers1

2

First, there is no substring-after-last() function in XSLT - not even in XSLT 3.0. And even if there were such a function, it would need two arguments: a string, and a delimiter. And - most importantly - I don't see how such function would be of any help here.

The problem with your input is that the EventTime values are not in a format that XSLT recognizes as time. In order to avoid the interim steps of converting them first to valid times and then to a sortable number or a string, you could use something like:

<xsl:template match="Case">
    <xsl:copy>
        <xsl:apply-templates select="CaseEvent">
            <!-- 1. AM before PM -->
            <xsl:sort select="substring-after(EventTime, ' ')" data-type="text" order="ascending"/>
            <!-- 2. Sort by hour (convert 12 to 0)-->
            <xsl:sort select="substring-before(EventTime, ':') mod 12" data-type="number" order="ascending"/>
            <!-- 3. Sort by minute -->
            <xsl:sort select="substring-before(substring-after(EventTime, ':'), ' ')" data-type="number" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51