anku csv to xml convertin logic

 UDF logic

def String formatWeight(String weight) {

    if (!weight) {

        return "0.000"

    }

 

    def df = new DecimalFormat("#####0.000")

    def weightValue = weight.trim()

 

    // Find decimal index

    int index = weightValue.indexOf('.')

 

    // If very small fractions like 0.000x → return 0.001

    if (index != -1 && weightValue.length() >= index + 4) {

        if (weightValue.substring(0, index + 4) == "0.000") {

            return "0.001"

        }

    }

 

    // If decimal exists → check fraction length

    if (index != -1) {

        String fractionValue = weightValue.substring(index + 1)

        if (fractionValue.length() > 3) {

            // Keep only first 3 decimals

            weightValue = weightValue.substring(0, index + 4)

 

            // Get the 4th decimal digit

            int pointerValue = fractionValue.charAt(3).toString().toInteger()

 

            // Round up if >=5

            if (pointerValue > 4) {

                weightValue = df.format(weightValue.toDouble() + 0.001)

            }

        }

    }

 

    return weightValue

}




-------------

<root>BRC     @BRC     @N@05249936@1865    @051124 ABR        @LEMON FLAVOR                                    @                  @R@      1000.00000@kg@

00000006@BRC     @                  @        @2029318587        @955632            @LINALOL LAEVO EX SHIU NAT                       @        50.00000@kg@FL2     @RACK    @ANUR    @2 @FL100   @LI      @RACK        @ @

</root>



----------------

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                xmlns:ns="http://firmenich/IMWM/TransferOrderCreation/Response"

                version="1.0">


  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>


  <!-- Root template -->

  <xsl:template match="/root">

    <ns:TransferOrderCreation>

      <!-- Extract the first line (header) -->

      <xsl:variable name="headerLine" select="substring-before(., '&#10;')" />

      <xsl:call-template name="Header">

        <xsl:with-param name="line" select="$headerLine"/>

      </xsl:call-template>


      <!-- Extract the second line (item) -->

      <xsl:variable name="rest" select="substring-after(., '&#10;')" />

      <xsl:variable name="itemLine" select="normalize-space($rest)" />

      <xsl:call-template name="Item">

        <xsl:with-param name="line" select="$itemLine"/>

      </xsl:call-template>

    </ns:TransferOrderCreation>

  </xsl:template>


  <!-- Header mapping -->

  <xsl:template name="Header">

    <xsl:param name="line"/>

    <Header>

      <Site><xsl:value-of select="normalize-space(substring-before($line,'@'))"/></Site>

      <xsl:variable name="f1" select="substring-after($line,'@')"/>

      <BMS_Id><xsl:value-of select="normalize-space(substring-before($f1,'@'))"/></BMS_Id>

      <xsl:variable name="f2" select="substring-after($f1,'@')"/>

      <Job_Status><xsl:value-of select="normalize-space(substring-before($f2,'@'))"/></Job_Status>

      <xsl:variable name="f3" select="substring-after($f2,'@')"/>

      <Order_Id><xsl:value-of select="normalize-space(substring-before($f3,'@'))"/></Order_Id>

      <xsl:variable name="f4" select="substring-after($f3,'@')"/>

      <BMS_Order_Id><xsl:value-of select="normalize-space(substring-before($f4,'@'))"/></BMS_Order_Id>

      <xsl:variable name="f5" select="substring-after($f4,'@')"/>

      <Product_Id><xsl:value-of select="normalize-space(substring-before($f5,'@'))"/></Product_Id>

      <xsl:variable name="f6" select="substring-after($f5,'@')"/>

      <Product_Name><xsl:value-of select="normalize-space(substring-before($f6,'@'))"/></Product_Name>

      <xsl:variable name="f7" select="substring-after($f6,'@')"/>

      <Lot_No><xsl:value-of select="normalize-space(substring-before($f7,'@'))"/></Lot_No>

      <xsl:variable name="f8" select="substring-after($f7,'@')"/>

      <Lot_No><xsl:value-of select="normalize-space(substring-before($f8,'@'))"/></Lot_No>

      <xsl:variable name="f9" select="substring-after($f8,'@')"/>

      <Weight_Total><xsl:value-of select="normalize-space(substring-before($f9,'@'))"/></Weight_Total>

      <xsl:variable name="f10" select="substring-after($f9,'@')"/>

      <Unit_Of_Measure><xsl:value-of select="normalize-space(substring-before($f10,'@'))"/></Unit_Of_Measure>

    </Header>

  </xsl:template>


  <!-- Item mapping -->

  <xsl:template name="Item">

    <xsl:param name="line"/>

    <Item>

      <Order_Pos_Id><xsl:value-of select="normalize-space(substring-before($line,'@'))"/></Order_Pos_Id>

      <xsl:variable name="f1" select="substring-after($line,'@')"/>

      <Site><xsl:value-of select="normalize-space(substring-before($f1,'@'))"/></Site>

      <xsl:variable name="f2" select="substring-after($f1,'@')"/>

      <Lot_No><xsl:value-of select="normalize-space(substring-before($f2,'@'))"/></Lot_No>

      <xsl:variable name="f3" select="substring-after($f2,'@')"/>

      <Lot_Ref><xsl:value-of select="normalize-space(substring-before($f3,'@'))"/></Lot_Ref>

      <xsl:variable name="f4" select="substring-after($f3,'@')"/>

      <Storage_Unit><xsl:value-of select="normalize-space(substring-before($f4,'@'))"/></Storage_Unit>

      <xsl:variable name="f5" select="substring-after($f4,'@')"/>

      <Product_Id><xsl:value-of select="normalize-space(substring-before($f5,'@'))"/></Product_Id>

      <xsl:variable name="f6" select="substring-after($f5,'@')"/>

      <Product_Name><xsl:value-of select="normalize-space(substring-before($f6,'@'))"/></Product_Name>

      <xsl:variable name="f7" select="substring-after($f6,'@')"/>

      <Weight_Actual><xsl:value-of select="normalize-space(substring-before($f7,'@'))"/></Weight_Actual>

      <xsl:variable name="f8" select="substring-after($f7,'@')"/>

      <Unit_Of_Measure><xsl:value-of select="normalize-space(substring-before($f8,'@'))"/></Unit_Of_Measure>

      <xsl:variable name="f9" select="substring-after($f8,'@')"/>

      <Automate_Id><xsl:value-of select="normalize-space(substring-before($f9,'@'))"/></Automate_Id>

      <xsl:variable name="f10" select="substring-after($f9,'@')"/>

      <Storage_Location_Id><xsl:value-of select="normalize-space(substring-before($f10,'@'))"/></Storage_Location_Id>

      <xsl:variable name="f11" select="substring-after($f10,'@')"/>

      <Dispensed_By><xsl:value-of select="normalize-space(substring-before($f11,'@'))"/></Dispensed_By>

      <xsl:variable name="f12" select="substring-after($f11,'@')"/>

      <Dosed_At><xsl:value-of select="normalize-space(substring-before($f12,'@'))"/></Dosed_At>

      <xsl:variable name="f13" select="substring-after($f12,'@')"/>

      <Family_Id><xsl:value-of select="normalize-space(substring-before($f13,'@'))"/></Family_Id>

      <xsl:variable name="f14" select="substring-after($f13,'@')"/>

      <Category><xsl:value-of select="normalize-space(substring-before($f14,'@'))"/></Category>

      <xsl:variable name="f15" select="substring-after($f14,'@')"/>

      <Address><xsl:value-of select="normalize-space(substring-before($f15,'@'))"/></Address>

      <xsl:variable name="f16" select="substring-after($f15,'@')"/>

      <Confirmation><xsl:value-of select="normalize-space(substring-before($f16,'@'))"/></Confirmation>

    </Item>

  </xsl:template>


</xsl:stylesheet>

------------------


Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

SAP CPI camel conditions and xpath conditions

oauth call to cpi integraiton suite from sap apim