Question

I'm trying to replace a 32 bit string variable. At first, all values "0".

$bitmask:="00000000000000000000000000000000"

I have some index values and should replace the values in these indexes with "1".

For instance, I have index values=(3,10)

expected result should be;

$bitmask:="00100000010000000000000000000000"

Actually I did it :) but there is space characters in my bitmask values. I couldn't remove the space characters.

My working code;

$serviceBits :=  tokenize('0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0',',')

<services>
{
for $t at $pos in $serviceBits
let  $temp := ''
return 
 if($pos = data($myElement/ns:position)) then
   concat($temp, '1')
 else  
   replace(concat($temp, $t)," ","")    
}
</services>

And the result of my working code is;

<services>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</services>
Was it helpful?

Solution

The problem in your code is that you post a sequence into the newly constructed element, which gets serialized with spaces in between. Explicitly use string-join here:

<services>{
  string-join(
    (: all the other code for modification :),
    '' (: Nothing between the individual strings :)
  )
}</services>

Anyway, I'm not sure where your call on tokenize and $bitmap are connected.

For converting an array to a sequence, adjusting some values and returning the string again, use string-to-codepoints respective the reverse function. It returns unicode codepoints, to change from 0 to 1 just add 1. An example:

let $bitmask := '00000000000000000000000000000000'
return
  codepoints-to-string(
    for $char at $i in string-to-codepoints($bitmask)
    return
      if ($i = (3,10))
        then $char + 1
        else $char
  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top