diff --git a/src/main/java/com/xmlcalabash/util/ProcessMatch.java b/src/main/java/com/xmlcalabash/util/ProcessMatch.java index 86f99c01..65fdbcfc 100644 --- a/src/main/java/com/xmlcalabash/util/ProcessMatch.java +++ b/src/main/java/com/xmlcalabash/util/ProcessMatch.java @@ -268,7 +268,7 @@ private void traverse(XdmNode node) throws SaxonApiException, XPathException { processor.processPI(node); saw = 0; } else { - addPI(node.getNodeName().getLocalName(), node.getStringValue()); + addPI(node); } } else { throw new UnsupportedOperationException("Unexpected node type"); diff --git a/src/main/java/com/xmlcalabash/util/TreeWriter.java b/src/main/java/com/xmlcalabash/util/TreeWriter.java index 9f2bcb5c..e9fbe760 100644 --- a/src/main/java/com/xmlcalabash/util/TreeWriter.java +++ b/src/main/java/com/xmlcalabash/util/TreeWriter.java @@ -139,28 +139,24 @@ public void endDocument() { } public void addSubtree(XdmNode node) { - try { - receiver.append(node.getUnderlyingNode()); - } catch (UnsupportedOperationException use) { - // Do it the hard way - if (node.getNodeKind() == XdmNodeKind.DOCUMENT) { - writeChildren(node); - } else if (node.getNodeKind() == XdmNodeKind.ELEMENT) { - addStartElement(node); - writeChildren(node); - addEndElement(); - } else if (node.getNodeKind() == XdmNodeKind.COMMENT) { - addComment(node.getStringValue()); - } else if (node.getNodeKind() == XdmNodeKind.TEXT) { - addText(node.getStringValue()); - } else if (node.getNodeKind() == XdmNodeKind.PROCESSING_INSTRUCTION) { - addPI(node.getNodeName().getLocalName(), node.getStringValue()); - } else { - throw new UnsupportedOperationException("Unexpected node type"); - } - } catch (XPathException xpe) { - throw new XProcException(xpe); - } + // N.B. It's tempting to copy the node with + // receiver.append(node.getUnderlyingNode()); + // but that doesn't work: https://saxonica.plan.io/issues/5691 + + if (node.getNodeKind() == XdmNodeKind.DOCUMENT) { + writeChildren(node); + } else if (node.getNodeKind() == XdmNodeKind.ELEMENT) { + addStartElement(node); + writeChildren(node); + addEndElement(); + } else if (node.getNodeKind() == XdmNodeKind.COMMENT) { + addComment(node.getStringValue()); + } else if (node.getNodeKind() == XdmNodeKind.TEXT) { + addText(node.getStringValue()); + } else if (node.getNodeKind() == XdmNodeKind.PROCESSING_INSTRUCTION) { + addPI(node.getNodeName().getLocalName(), node.getStringValue()); + } else { + throw new UnsupportedOperationException("Unexpected node type"); } } protected void writeChildren(XdmNode node) { @@ -336,11 +332,24 @@ public void addText(String text) { } public void addPI(String target, String data) { + addPI(target, data, VoidLocation.instance()); + } + + public void addPI(String target, String data, Location location) { try { - receiver.processingInstruction(target, data, VoidLocation.instance(), 0); + receiver.processingInstruction(target, data, location, 0); } catch (XPathException e) { throw new XProcException(e); } } + + public void addPI(XdmNode node) { + Location location = VoidLocation.instance(); + if (node.getBaseURI() != null) { + location = new SysIdLocation(node.getBaseURI().toString()); + receiver.setSystemId(node.getBaseURI().toString()); + } + addPI(node.getNodeName().getLocalName(), node.getStringValue(), location); + } }