From d1610d246b0cc9e0cfa85c78964c3c841a10b0eb Mon Sep 17 00:00:00 2001 From: Philip Helger Date: Thu, 17 Nov 2022 09:01:01 +0100 Subject: [PATCH] Avoid potential double namespace prefix; #31 --- .../src/main/java/com/helger/xml/XMLHelper.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ph-xml/src/main/java/com/helger/xml/XMLHelper.java b/ph-xml/src/main/java/com/helger/xml/XMLHelper.java index 5d5ac2862..3569d9b42 100644 --- a/ph-xml/src/main/java/com/helger/xml/XMLHelper.java +++ b/ph-xml/src/main/java/com/helger/xml/XMLHelper.java @@ -566,7 +566,18 @@ private static String _getPathToNode (@Nonnull final Node aNode, break; } - final StringBuilder aName = new StringBuilder (aCurNode.getNodeName ()); + final String sNamespaceURI = aCurNode.getNamespaceURI (); + + final StringBuilder aName = new StringBuilder (); + if (nNodeType == Node.ATTRIBUTE_NODE) + aName.append ('@'); + if (StringHelper.hasText (sNamespaceURI)) + { + aName.append (funGetNSPrefix.apply (sNamespaceURI)); + aName.append (aCurNode.getLocalName ()); + } + else + aName.append (aCurNode.getNodeName ()); final Node aParentNode; if (nNodeType == Node.ATTRIBUTE_NODE) @@ -633,11 +644,7 @@ private static String _getPathToNode (@Nonnull final Node aNode, // Avoid trailing separator aRet.insert (0, sSep); } - aRet.insert (0, aName); - aRet.insert (0, funGetNSPrefix.apply (aCurNode.getNamespaceURI ())); - if (nNodeType == Node.ATTRIBUTE_NODE) - aRet.insert (0, '@'); // goto parent aCurNode = aParentNode;