This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 7e6f90a24cf Only treat xmlns declarations as prefixes in
W3CNamespaceContext.getP… (#3790)
7e6f90a24cf is described below
commit 7e6f90a24cfeaa409b837d1e9a5b32658f3f653a
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 27 08:30:34 2026 +0000
Only treat xmlns declarations as prefixes in W3CNamespaceContext.getP…
(#3790)
* Only treat xmlns declarations as prefixes in W3CNamespaceContext.getPrefix
* Review comment
---
.../apache/cxf/staxutils/W3CNamespaceContext.java | 8 +++-
.../cxf/staxutils/W3CNamespaceContextTest.java | 54 ++++++++++++++++++++++
2 files changed, 60 insertions(+), 2 deletions(-)
diff --git
a/core/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
b/core/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
index c2ece6b1c55..5acee2e6b17 100644
--- a/core/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
+++ b/core/src/main/java/org/apache/cxf/staxutils/W3CNamespaceContext.java
@@ -96,11 +96,15 @@ public class W3CNamespaceContext implements
NamespaceContext {
String val = a.getValue();
if (val != null && val.equals(uri)) {
- String name = a.getLocalName();
+ // only namespace declarations bind a prefix
+ String name = a.getName();
if ("xmlns".equals(name)) {
return "";
}
- return name;
+ if (name.startsWith("xmlns:")) {
+ return name.substring(6);
+ }
+ // any other attribute is not a namespace declaration:
keep looking
}
}
}
diff --git
a/core/src/test/java/org/apache/cxf/staxutils/W3CNamespaceContextTest.java
b/core/src/test/java/org/apache/cxf/staxutils/W3CNamespaceContextTest.java
new file mode 100644
index 00000000000..9bba4b082a4
--- /dev/null
+++ b/core/src/test/java/org/apache/cxf/staxutils/W3CNamespaceContextTest.java
@@ -0,0 +1,54 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cxf.staxutils;
+
+import java.io.StringReader;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.cxf.helpers.DOMUtils;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class W3CNamespaceContextTest {
+
+ private static Element parse(String xml) throws Exception {
+ Document doc = StaxUtils.read(new StringReader(xml));
+ return doc.getDocumentElement();
+ }
+
+ @Test
+ public void testGetPrefixIgnoresNonNamespaceAttributes() throws Exception {
+ Element root = parse("<root
targetNamespace=\"urn:a\"><child/></root>");
+ W3CNamespaceContext ctx = new
W3CNamespaceContext(DOMUtils.getFirstElement(root));
+ assertNull(ctx.getPrefix("urn:a"));
+ }
+
+ @Test
+ public void testGetPrefixFromDeclarations() throws Exception {
+ Element root = parse("<root xmlns:a=\"urn:a\" xmlns=\"urn:d\"
other=\"urn:d\"><child/></root>");
+ W3CNamespaceContext ctx = new
W3CNamespaceContext(DOMUtils.getFirstElement(root));
+ assertEquals("a", ctx.getPrefix("urn:a"));
+ assertEquals("", ctx.getPrefix("urn:d"));
+ }
+}