Home > Software design >  xsl transformer to fetch highest value
xsl transformer to fetch highest value

Time:09-29

I have the below XML input file

<?xml version="1.0" encoding="UTF-8"?>
<ns3:oppgjor xmlns:ns3="http://NEXSTEP.Schemas.Kasseoppgjor" xmlns:ns2="http://NEXSTEP.Schemas.NexstepTypes">
  <ROUTE_RECORD>
    <ns2:SENDER Sender_code="BO_SERVER"/>
    <ns2:RECEIPIENT Receipient_code="MASTER"/>
    <ns2:MESSAGE_ID Type_name="Bonger" Version_number="1.0"/>
  </ROUTE_RECORD>
  <MESSAGE_RECORD>
    <InsertChange>
      <id>fb920f1d-3d14-4f45-b142-1e6df39bf17e</id>
      <bruker>000821</bruker>
      <kasseId>T10</kasseId>
      <opprettetTidspunkt>2021-08-26T09:48:24.423 02:00</opprettetTidspunkt>
      <startBeholdning>1500</startBeholdning>
      <opptaltBeholdning>-4990</opptaltBeholdning>
      <beregnetBeholdning>988</beregnetBeholdning>
      <avdeling>0</avdeling>
      <lager>STA</lager>
      <bonger>
        <bong>T10-54</bong>
        <bong>T10-53</bong>
        <bong>T10-49</bong>
        <bong>T10-66</bong>
        <bong>T10-17</bong>
        <bong>T10-16</bong>
        <bong>T10-15</bong>
        <bong>T10-14</bong>
        <bong>T10-13</bong>
        <bong>T10-12</bong>
      </bonger>
    </InsertChange>
  </MESSAGE_RECORD>
</ns3:oppgjor>

My output should be as below

<?xml version="1.0" encoding="UTF-8"?>
<CASH_REGISTER_REPORT>
   <REFERENCE>66</REFERENCE>
</CASH_REGISTER_REPORT>

Reference 66 should be calculated via path MESSAGE_RECORD/InsertChange/bonger/bong. This should be the value after '-' and the highest value should be taken. According to the example it should be 66.

T10-66 => 66

Below is my current XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns3="http://NEXSTEP.Schemas.Kasseoppgjor" xmlns:ns2="http://NEXSTEP.Schemas.NexstepTypes" exclude-result-prefixes="ns2 ns3">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="*">
        <xsl:element name="{name()}" namespace="{namespace-uri()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="/ns3:oppgjor">
        <CASH_REGISTER_REPORT>
          <REFERENCE></REFERENCE>
        </CASH_REGISTER_REPORT>
    </xsl:template>
</xsl:stylesheet>

Is there a way to easily fetch the highest value in MESSAGE_RECORD/InsertChange/bonger/bong to REFERENCE?

Cheers

CodePudding user response:

Here is one way you could look at it:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns3="http://NEXSTEP.Schemas.Kasseoppgjor"
exclude-result-prefixes="ns3">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/ns3:oppgjor">
    <CASH_REGISTER_REPORT>
        <xsl:for-each select="MESSAGE_RECORD/InsertChange/bonger/bong">
            <xsl:sort select="substring-after(., '-')" data-type="number" order="descending"/>
            <xsl:if test="position()=1">
                <REFERENCE>
                    <xsl:value-of select="substring-after(., '-')"/>
                </REFERENCE>
            </xsl:if>
        </xsl:for-each>
    </CASH_REGISTER_REPORT>
</xsl:template>

</xsl:stylesheet>
  • Related