From 85b1f2d4c4f0f6478e78fe5bc8ec58aa7e82ce91 Mon Sep 17 00:00:00 2001 From: Norman Walsh Date: Mon, 19 Sep 2022 16:04:33 +0100 Subject: [PATCH] Fix #338 don't lose base URI when copying PIs --- .../com/xmlcalabash/util/ProcessMatch.java | 2 +- .../java/com/xmlcalabash/util/TreeWriter.java | 54 +++++++++++-------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/xmlcalabash/util/ProcessMatch.java b/src/main/java/com/xmlcalabash/util/ProcessMatch.java index f97e7ad2..b647ee45 100644 --- a/src/main/java/com/xmlcalabash/util/ProcessMatch.java +++ b/src/main/java/com/xmlcalabash/util/ProcessMatch.java @@ -264,7 +264,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 93d6fd69..e4042be7 100644 --- a/src/main/java/com/xmlcalabash/util/TreeWriter.java +++ b/src/main/java/com/xmlcalabash/util/TreeWriter.java @@ -141,27 +141,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"); } } @@ -338,11 +335,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, StringView.of(data), VoidLocation.instance(), 0); + receiver.processingInstruction(target, StringView.of(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); + } }