Move lines 13-15 to before the loop; they are called for the node itself, not the child nodes.
Be warned though that the cast to Element may fail; whitespace like line breaks are often seen as Text nodes, and will show up in the child node list.