none
Find the field with highest number - xslt? RRS feed

  • Question

  • I have below message processed in BizTalk, my requirement is I need to fetch "EmployeeNumber" field which has the max number and find its related fields under it.
    How can I achieve this scenario ?

    <?xml version="1.0" encoding="utf-8"?>
    <ns0:jsTest xmlns:ns0="http://TestSchema.TestSchemaJ">
      <Files>
        <Employee>
          <EmpPosition>1</EmpPosition>
          <EmpType>Developer</EmpType>
        </Employee>
        <EmployeeNumber>725A</EmployeeNumber>
        <EmpID>0780</EmpID>
        <Status>
          <StatusID>1</StatusID>
          <Name>Open</Name>
        </Status>    
      </Files>
      
      <Files>
        <Employee>
          <EmpPosition>2</EmpPosition>
          <EmpType>Lead</EmpType>
        </Employee>
        <EmployeeNumber>725A-1</EmployeeNumber>
        <EmpID>0781</EmpID>
        <Status>
          <StatusID>2</StatusID>
          <Name>Canceled</Name>
        </Status>    
      </Files>
      
      <Files>
        <Employee>
          <EmpPosition>3</EmpPosition>
          <EmpType>Manager</EmpType>
        </Employee>
        <EmployeeNumber>725A-2</EmployeeNumber>
        <EmpID>0782</EmpID>
        <Status>
          <StatusID>5</StatusID>
          <Name>Pending</Name>
        </Status>    
      </Files>  
     
        <Files>
        <Employee>
          <EmpPosition>4</EmpPosition>
          <EmpType>Director</EmpType>
        </Employee>
        <EmployeeNumber>725A-3</EmployeeNumber>
        <EmpID>0783</EmpID>
        <Status>
          <StatusID>8</StatusID>
          <Name>Closed</Name>
        </Status>    
      </Files>
     </ns0:jsTest>

      I want Output to find out the max EmployeeNumber and its related fields under that record

    <ns0:Ascending xmlns:ns0="http://Test.Output">
      <EmployeeNumber>725A-3</EmployeeNumber>
      <EmpID>0783</EmpID>
      <Name>Closed</Name>
    </ns0:Ascending>




    • Edited by Dan2890 Thursday, December 5, 2019 4:45 PM a
    Tuesday, December 3, 2019 6:53 PM

All replies

  • I tried below inLine XSLT, it didnt work. Can any XSLT experts advise

    <xsl:element name="EmployeeNumber">
          <xsl:for-each select ="Files">
            <xsl:sort select="EmployeeNumber" datatype="text" order="descending"/>
            <xsl:if test="position() = 1">
              <Row>
                <xsl:copy-of select="EmployeeNumber | EmpID | Status/Name"/>
              </Row>
            </xsl:if>
          </xsl:for-each>
    
     </xsl:element>


    RH

    Thursday, December 5, 2019 4:42 PM
  • This works for me:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="/">
        <A>
          <xsl:for-each select="//Files">
            <xsl:sort select="EmployeeNumber" order="descending" />
            <xsl:if test="position() = 1">
              <Row>
                <xsl:copy-of select="EmployeeNumber | EmpID | Status/Name" />
              </Row>
            </xsl:if>
          </xsl:for-each>
        </A>
      </xsl:template>
    </xsl:stylesheet>

    Morten la Cour

    Thursday, December 5, 2019 5:37 PM
  • I tried above, but it throws error as : There are multiple root elements

    I am using below xsl as my inLine XSLT. Please advise

     <Ascending>
          <xsl:for-each select="//Files">
            <xsl:sort select="EmployeeNumber" order="descending" />
            <xsl:if test="position() = 1">
              <Ascending>
                <xsl:copy-of select="EmployeeNumber | EmpID | Status/Name" />
              </Ascending>
            </xsl:if>
          </xsl:for-each>
        </Ascending>



    • Edited by Dan2890 Thursday, December 5, 2019 6:42 PM a
    Thursday, December 5, 2019 6:37 PM
  • XSLT experts can anyone please advise

    RH

    Friday, December 6, 2019 3:39 PM
  • XSLT is reading the value 1 as 10, 2 as 20...

    How can I arrange and fix this ?

    for e.g... sorting is done like below

    <EmpID>ABC-8</EmpID>

    <EmpID>ABC-9</EmpID>

    <EmpID>ABC-1</EmpID>

    <EmpID>ABC-10</EmpID>

    <EmpID>ABC-11</EmpID>

    <EmpID>ABC-18</EmpID>

    <EmpID>ABC-19</EmpID>

    <EmpID>ABC-2</EmpID>

    <EmpID>ABC-20</EmpID>

    <EmpID>ABC-21</EmpID>


    RH


    • Edited by Dan2890 Tuesday, December 10, 2019 4:43 PM a
    Tuesday, December 10, 2019 3:52 PM
  • Instead of 

    <xsl:sort select="EmpID"

    use:

    <xsl:sort select="number(EmpID)"

    Morten la Cour

    Thursday, December 12, 2019 6:05 AM