I am trying to select specific elements from an XML document to make anthologies, and still retain the original numbering, by using \xmldoifelse; if the node matches, it is printed, and if it is not, the head number is incremented, to get something like this:

    1 First Section (first match)
    4 Fourth Section (second match, but fourth element in the XML file)

 But the fail condition is not being triggered properly. Here are the files:

%
% anthologize.xml
%
\def\incrementheadnumber[#1]{\setupheadnumber[#1]
                        [\numexpr\namedheadnumber{#1}+1\relax]}

\startxmlsetups xml:selectivity:*
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{stuff|div|head}{xml:selectivity:*}
\stopxmlsetups

\xmlregisterdocumentsetup{selectivity}{xml:selectivity:*}

% Here is the difficulty.
\startxmlsetups{xml:selectivity:stuff}
    \xmldoifelse{#1}{/div[match()==1 or match()==4]/all()}
    {
      \xmlflush{#1}
    } {
      \incrementheadnumber[section]
    }
\stopxmlsetups

\startxmlsetups{xml:selectivity:div}
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups{xml:selectivity:head}
  \startsection[title={\xmlflush{#1}}]\stopsection
\stopxmlsetups

\starttext

  \xmlprocessfile{selectivity}{bob.xml}{}

\stoptext

%
% bob.xml
%
<stuff>
  <div>
    <head>First Section</head>
  </div>
  <div>
    <head>Second Section</head>
  </div>
  <div>
    <head>Third Section</head>
  </div>
  <div>
    <head>Fourth Section</head>
  </div>
</stuff>

The result is that only the first and fourth heads are printed, as desired, but I end up with:

    1 First Section
    2 Fourth Section.

I have used the \incrementheadnumber function elsewhere, and it works fine. If I replace \incrementheadnumber[section] with \xmlflush{#1}, it prints:

    1 First Section
    2 Fourth Section
    3 First Section
    4 Second Section
    5 Third Section
    6 Fourth Section

So it seems to be properly triggering the true condition, but not the false condition, and I cannot figure out how to fix it. Using ConTeXt 2017.04.08 and LuaTeX 1.0.3. I do thank you for your help.

Sincerely,
Gerard Keiser