1

I am trying to create a XSLT in 1.0.PLease find below a sample xml. The requirement is to get the biggest value1 field from all 3 and display the tags inside them.

Request XML:

<?xml version="1.0" encoding="UTF-8"?>
<GetTestValues xmlns="http://www.test.com/GetTestValues">
  <TestSample>
    <Test>
      <value1>10000</value1>
      <value2>2016-01-28</value2>
      <value3>2017-12-10T01:36:12.403+00:00</value3>
      <value4>US</value4>
      <value5>true</value5>
    </Test>
    <Test>
      <value1>30000</value1>
      <value2>2019-01-28</value2>
      <value3>2019-01-28T18:04:13.763+00:00</value3>
      <value4>Canada</value4>
      <value5>false</value5>
    </Test>
    <Test>
      <value1>50000</value1>
      <value2>2019-01-28</value2>
      <value3>2019-01-28T18:04:13.763+00:00</value3>
      <value4>Canada</value4>
      <value5>false</value5>
    </Test>
  </TestSample>
</GetTestValues>

Expected Response after transformation:

Request XML:

<?xml version="1.0" encoding="UTF-8"?>
<GetTestValues xmlns="http://www.test.com/GetTestValues">
  <TestSample>
    <Test>
      <value1>50000</value1>
      <value2>2019-01-28</value2>
      <value3>2019-01-28T18:04:13.763+00:00</value3>
      <value4>Canada</value4>
      <value5>false</value5>
    </Test>
  </TestSample>
</GetTestValues>

I tried to put foreach and it is displaying everything. Not sure how to compare the get the great value.

<xsl:for-each select="/ns0:GetTestValues/ns0:TestSample/ns0:Test">
  <ns0:value1>
    <xsl:value-of select="ns0:value1"/>
  </ns0:value1>
  <ns0:value2>
    <xsl:value-of select="ns0:value2"/>
  </ns0:value2>
  <ns0:value3>
    <xsl:value-of select="ns0:value3"/>
  </ns0:value3>
  <ns0:value4>
    <xsl:value-of select="ns0:value4"/>
  </ns0:value4>
  <ns0:value5>
    <xsl:value-of select="ns0:value5"/>
  </ns0:value5>
</xsl:for-each>
Siebe Jongebloed
  • 3,906
  • 2
  • 14
  • 19
Krishna
  • 13
  • 2
  • 2
    Welcome to stackoverflow. Please checkout the [tour], [editing help](https://stackoverflow.com/editing-help) and [ask]. Edit your question to make it legible. – Marcelo Paco Apr 14 '23 at 05:05
  • I don't see where grouping comes into this. To get the node with the maximum value (in XSLT 1.0) you need to sort the nodes by value and take the first (or the last) node - see an example here: https://stackoverflow.com/a/30848387/3016153 and here: https://stackoverflow.com/a/28359107/3016153. – michael.hor257k Apr 14 '23 at 05:30
  • Unfortunately your question is not really readable. The easiest way to produce readable code on StackOverflow is to use a row of three back-ticks to start and end each of your code blocks. See https://stackoverflow.com/help/formatting – Conal Tuohy Apr 14 '23 at 05:44

1 Answers1

0

In this stylesheet, I iterate over the set of Test elements, in descending order of their value1 child elements (so that the first Test element has the highest value1). Inside the for-each I check the current position of the iteration, and if it's 1 then I copy the current Test element; the other Test elements are simply ignored.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:test="http://www.test.com/GetTestValues">

    <xsl:output method="xml" indent="yes" />

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

    <!-- Select only the Test child whose value1 is greatest -->
    <xsl:template match="test:TestSample">
        <xsl:copy>
            <!-- iterate over the test child elements, in descending order of value1 -->
            <xsl:for-each select="test:Test">
                <xsl:sort select="test:value1" order="descending"/>
                <!-- copy the test element if it's the first (has the highest value1) -->
                <xsl:if test="position()=1">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
Conal Tuohy
  • 2,561
  • 1
  • 8
  • 15