pdu.appendChild(elem1);
String expected = "<n1:elem1"
+ " xmlns:n1=\"http://b.example\">content</n1:elem1>";
ByteArrayOutputStream out = new ByteArrayOutputStream();
Canonicalizer canonicalizer = new Canonicalizer(out,
Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS);
XPathContext context = new XPathContext("n1", "http://b.example");
Document doc = new Document(pdu);
canonicalizer.setInclusiveNamespacePrefixList("n0");
canonicalizer.setInclusiveNamespacePrefixList(null);
Nodes subset = doc.query(
"(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]",
context);
canonicalizer.write(subset);
byte[] result = out.toByteArray();
out.close();
String s = new String(out.toByteArray(), "UTF8");
assertEquals(expected, s);