String expected = "<pre:child312 xmlns:pre=\"http://www.example.org/\"></pre:child312>";
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
XPathContext context = new XPathContext("pre", "http://www.example.org/");
Canonicalizer serializer = new Canonicalizer(out,
Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION);
serializer.write(doc.query("/*/pre:child312 | /*/pre:child312/namespace::node()", context));
}
finally {
out.close();
}