using xquery how to loop thru characters in a string and remove leading zeros

34 views Asked by At

I have a requirement to remove leading zeros from a string value.

Example:

  • 00012MD78009850 should become: 12MD78009850
  • 78CA65430 should be kept the same: 78CA65430

My request is in the form of an XML:

<order>
<itemId>00012MD78009850</itemId>

</order>

I need to parse the value inside itemId and show 00012MD78009850 as 12MD78009850.

I tried to use:

fn-bea:trim-left(fn:replace(data(order/itemId), '0' ,''))

but this replaced all zeros and got the incorrect result 12MD78985.

1

There are 1 answers

0
The real Van Gogh On

You can try this:

fn:replace(data(order/itemId), '^0+', '')

The ^ means process everything at the beginning of a string

The 0 is the character you would like to process (remove, trim)

The + one or more occurrences