int rootelems = 0;
for (XML r = x; r != null; r = r.getNextSibling())
if (r.isElement())
rootelems++;
else if (r.isText()) {
Text t = r.asText();
if (!StringTypes.isWhitespace(t.getString()))
throw new XMLWellformednessException("non-whitespace text at root", t.getOrigin());
}
if (rootelems != 1)
throw new XMLWellformednessException("wrong number of root elements", x.getOrigin());
}
final Stack<Entry> stack = new Stack<Entry>(); // using heap stack, avoids deep recursive calls