anchor.diff:           xqerl_node:new_fragment(Ctx, Frag)
      end.
   
  -analyze_string1([],String) -> % no matches
  +analyze_string1([],String, _) -> % no matches
      #xqElementNode{name = #qname{namespace = ?NS,
                                   prefix = ?PX,
                                   local_name = ?A("non-match")},
                            content = ?str(String)};
  -analyze_string1(List,String) ->
  +analyze_string1(List, String, GrpTree) ->
      Fun = 
  -     fun([{Start,End}|Groups],LastPos) ->
  +     fun([{Start, End}|Groups], LastPos) ->
  +            NextPos = Start + End,
  +           % leading non-match node
              Pre = if Start =/= LastPos ->
  -                       Slc = string:slice(String,LastPos,(Start-LastPos)),
  -                       [analyze_string1([],Slc)];
  +                       Slc = string:slice(String, LastPos, (Start - LastPos)),
  +                       [analyze_string1([], Slc, [])];
                       true ->
                          []
                    end,
              Matches = 
                 case Groups of
                   [] ->
  -                   S = string:slice(String,Start,End),
  +                   S = string:slice(String, Start, End),
                      #xqTextNode{string_value = ?str(S)};
                   _ ->
  -                   {B,_} = hd(Groups),
  +                   {Groups1, _} = fill_groups(GrpTree, Groups),
                      {Es,Ee} = hd(lists:reverse(Groups)),
                      GrpSize = Es+Ee,
  -                   Tail = if (Start + End) > GrpSize -> % missing tail text 
  +                   Tail = if (Start + End) > GrpSize -> % non-grouped tail match 
                                   Slc1 = string:slice(String,GrpSize,
                                                       Start + End - GrpSize),
                                   [#xqTextNode{string_value = ?str(Slc1)}];
                                true ->
                                   []
                             end,
  -                    if B > Start ->
  -                          S = string:slice(String,Start,Start + End - B),
  -                          [#xqTextNode{string_value = ?str(S)} |
  -                             get_groups(String,Groups,1)] ++ Tail;
  -                       true ->
  -                          get_groups(String,Groups,1) ++ Tail
  -                    end
  +                   GEls = get_groups(String,Groups1, Start), 
  +                   GEls ++ Tail
                 end,
              Match = #xqElementNode{name = #qname{namespace = ?NS,
                                                   prefix = ?PX,
  
positive.code: 'analyze-string'(Ctx,Input,Pattern0,Flags) ->
     Pattern = xqerl_types:string_value(Pattern0),
     Flags1 = xqerl_types:string_value(Flags),
     case xs_regex:compile(Pattern,Flags1) of
        {error, {invalid_flag, _}} ->
           ?dbg("invalid_flag",invalid_flag),
           ?err('FORX0001');
        {error, Err} ->
           ?dbg("Err",Err),
           ?err('FORX0002');
        {true,_} ->
           ?dbg("true",true),
           ?err('FORX0003');
        {_,MP} ->
           Input1 = string_value(Input),
           Content = case re:run(Input1, MP, [global]) of
                        nomatch ->
  
negative.code:                    end,
           Expr = if Input1 == <<>> -> [];
                     true ->
                        analyze_string1(Content, Input1, Grps)
                  end,
           Frag = #xqElementNode{name = #qname{namespace = ?NS,
                                               prefix = ?PX,
  