How can I increment the x variable only when numerical is false? (I've been reading how xquery isn't iterative...)

current output:

<xml>
  <person id="1" x="0" numerical="false">people</person>
  <person id="2" x="0" numerical="false">joe</person>
  <person id="3" x="0" numerical="true">phone1</person>
  <person id="4" x="0" numerical="true">phone2</person>
  <person id="5" x="0" numerical="true">phone3</person>
  <person id="6" x="0" numerical="false">sue</person>
  <person id="7" x="0" numerical="true">cell4</person>
  <person id="8" x="0" numerical="true">home5</person>
  <person id="9" x="0" numerical="false">alice</person>
  <person id="10" x="0" numerical="true">atrib6</person>
  <person id="11" x="0" numerical="true">x7</person>
  <person id="12" x="0" numerical="true">y9</person>
  <person id="13" x="0" numerical="true">z10</person>
</xml>

desired output:


<xml>
  <person id="1" x="1" numerical="false">people</person>
  <person id="2" x="2" numerical="false">joe</person>
  <person id="3" x="2" numerical="true">phone1</person>
  <person id="4" x="2" numerical="true">phone2</person>
...
</xml>

Maybe with a second xquery?  Here's the first:

xquery version "3.0";

<xml>

{
variable $x:=0;

for $line in db:open("foo.txt")//text()


count $id

return    if (matches($line, "[0-9]"))
         then <person id='{$id}' x='{$x}' numerical="true">{$line}</person>
else <person id='{$id}' x='{$x}' numerical="false">{$line}</person>
}
</xml>



where I'm trying to use attributes because I'm not sure how to conditionally nest tags. But, this is interesting. Not quite sure on syntax to set and then conditionally increment $x, however.


thanks,

Thufir

Reply via email to