Skip to content
Permalink
Browse files
[JXPATH-154] handling of inner empty/default ns in DOM XML model
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jxpath/trunk@1234036 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbenson committed Jan 20, 2012
1 parent 5c538c4 commit afa2deca94436837e785b89362cfaaf84a8ca1bd
Showing 3 changed files with 63 additions and 15 deletions.
@@ -677,24 +677,23 @@ public static String getNamespaceURI(Node node) {
Element element = (Element) node;

String uri = element.getNamespaceURI();
if (uri != null) {
return uri;
}

String prefix = getPrefix(node);
String qname = prefix == null ? "xmlns" : "xmlns:" + prefix;

Node aNode = node;
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
Attr attr = ((Element) aNode).getAttributeNode(qname);
if (attr != null) {
return attr.getValue();
if (uri == null) {
String prefix = getPrefix(node);
String qname = prefix == null ? "xmlns" : "xmlns:" + prefix;

Node aNode = node;
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
Attr attr = ((Element) aNode).getAttributeNode(qname);
if (attr != null) {
uri = attr.getValue();
break;
}
}
aNode = aNode.getParentNode();
}
aNode = aNode.getParentNode();
}
return null;
return "".equals(uri) ? null : uri;
}

public Object getValue() {
@@ -0,0 +1,22 @@
<?xml version="1.0" ?>
<!--
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.
-->

<b:foo xmlns:b="bla" xmlns="test111">
<b:bar>a</b:bar>
<test xmlns=""></test>
</b:foo>
@@ -0,0 +1,27 @@
package org.apache.commons.jxpath.ri.model;

import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathTestCase;
import org.apache.commons.jxpath.xml.DocumentContainer;

public class JXPath154Test extends JXPathTestCase {

protected JXPathContext context;

protected DocumentContainer createDocumentContainer(String model) {
return new DocumentContainer(JXPathTestCase.class.getResource("InnerEmptyNamespace.xml"), model);
}

protected void doTest(String path, String model, String expectedValue) {
JXPathContext context = JXPathContext.newContext(createDocumentContainer(model));
assertEquals(expectedValue, context.getPointer(path).asPath());
}

public void testInnerEmptyNamespaceDOM() {
doTest("b:foo/test", DocumentContainer.MODEL_DOM, "/b:foo[1]/test[1]");
}

public void testInnerEmptyNamespaceJDOM() {
doTest("b:foo/test", DocumentContainer.MODEL_JDOM, "/b:foo[1]/test[1]");
}
}

0 comments on commit afa2dec

Please sign in to comment.