Elements inputs = root.getChildElements("document");
Element input = inputs.get(1);
Document html = new Document(new Element("fake"));
int p = 0;
while (true) {
Node node = input.getChild(0);
if (node instanceof Element) break;
else {
node.detach();
if (node instanceof Text) continue;
html.insertChild(node, p++);
}
}
Node newroot = input.getChild(0);
newroot.detach();
html.setRootElement((Element) newroot);
while (input.getChildCount() > 0) {
Node node = input.getChild(0);
node.detach();
if (node instanceof Text) continue;
html.appendChild(node);
}
Nodes doc2Queries = root.query("child::query[starts-with(@id, 'A')]");
XPathContext context = new XPathContext();
context.addNamespace("svg", "http://www.w3.org/2000/svg");
context.addNamespace("xlink", "http://www.w3.org/1999/xlink");
for (int i = 0; i < doc2Queries.size(); i++) {
Element query = (Element) doc2Queries.get(i);
String xpath = query.getFirstChildElement("syntax").getValue();
String id = query.getAttributeValue("id");
Nodes result = html.query(xpath, context);
Element answer = query.getFirstChildElement("answer");
Nodes expected = new Nodes();
for (int j = 0; j < answer.getChildCount(); j++) {
Node node = answer.getChild(j);
if (node instanceof Text) {
if (!("".equals(node.getValue().trim()))) {
expected.append(node);
}
}
else {
expected.append(node);
}
}
assertEquals("Failed query " + id, expected.size(), result.size());
for (int j = 0; j < result.size(); j++) {
Node expectedNode = expected.get(j);
Node actualNode = result.get(j);
assertEquals(id + " " + expectedNode.toXML() + " "
+ actualNode.toXML(), expectedNode, actualNode);
}
}
}