2 Replies Latest reply on Oct 8, 2019 3:51 PM by Martien van den Akker

    Adding a zero to a string using "current()" in "format-number"

    Jesper!

      Hi Oracle friends,

      I have an xml element <RandomNumber> which string size is always either 8 or 9 digits. If the element has a string-length greater than 1, I wish to left pad it with a zero if its size is 8 instead of 9 digits. So I wrote the following code:

      <?xml version = '1.0' encoding = 'UTF-8'?>
          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
          <xsl:output method="xml"/>
          <xsl:variable name="STRING_LENGTH">
                <xsl:text>1</xsl:text>
          </xsl:variable>
          <xsl:for-each select="current()[string-length(RandomNumber) >= $STRING_LENGTH]/RandomNumber">
              <xsl:element name="leftPNumber">
                  <xsl:value-of select="format-number(current(), '000000000')"/>
              </xsl:element>
          </xsl:for-each>
          </xsl:stylesheet>
      

      For the input:

      <RandomNumbers> <RandomNumber>11223366</RandomNumber> </RandomNumbers>

      The intended output should be:

      <leftPNumber>011223366</leftPNumber>

      For some reason it doesnt work as intended, but doesnt fault either. It just creates the new element with the original number in it, without adding the additional "0". Is this because im using current() inside the "format-number"? If so, what alternative can I use?

      Cheers,

      Jesper

        • 1. Re: Adding a zero to a string using "current()" in "format-number"
          Martien van den Akker

          Hi Jesper,

           

          If I take a look at https://www.w3schools.com/xml/func_formatnumber.asp then the problem is that you provide only zeroes as a format.

          Try this:

          <xsl:element name="leftPNumber">
          
              <xsl:value-of select="format-number(., '########0')"/> 
          </xsl:element>
          

           

          So, for every position include a hash ('#') except for the lowest decimal place.

          A few other remarks:

          • the function current() can be replaced by a dot ('.'). This means exactly the same, but is better readable.
          • 'current()[string-length(RandomNumber) >= $STRING_LENGTH]/RandomNumber' is a very complex way to say 'RandomNumber[string-length(.) >= $STRING_LENGTH]'
          • $STRING_LENGTH is a global variable containing '1'. But in most cases you probably want to check if the element (in this case RandomNumber) exists and has a value. So, I guess in most cases  ' =>$STRING_LENGTH' can safely be replaced by just '>0'. Using the variable is a way to be able to change the value for all the tests in your xslt. But, since you actually mean 'check if this element exist and is not empty', functionally you would never change the value of STRING_LENGTH. Therefor checking against string-length()>0 is not only less elaborate, but also clearer what is meant.

          This means that you code can be simply:

           

          <?xml version = '1.0' encoding = 'UTF-8'?>  
              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
              <xsl:output method="xml"/>  
              <xsl:for-each select="RandomNumber[string-length(.) >0]">  
                  <xsl:element name="leftPNumber">  
                      <xsl:value-of select="format-number(., '########0')"/>  
                  </xsl:element>  
              </xsl:for-each>  
          </xsl:stylesheet> 
          

           

          Hope this helps.

           

          Kind regards,

          Martien

          1 person found this helpful
          • 2. Re: Adding a zero to a string using "current()" in "format-number"
            Martien van den Akker

            By the way, if the xslt is in the MDS, you might need to do a redeploy of a/the composite that uses it, to refresh the cached xslt.

             

            Regards,
            Martien

            1 person found this helpful