Java XML QName resolveNamespace(String qname, Node xml)

Here you can find the source of resolveNamespace(String qname, Node xml)

Description

Parses the prefix from the given qualified name and finds the first XML namespace declaration that maps that prefix to a namespace URI.

License

Apache License

Parameter

Parameter Description
qname The qualified name whose prefix is searched for.
xml The Node from which namespace resolution will start.

Return

The namespace URI that the QName's prefix is associated with. The method returns null if no match is found.

Declaration

public static String resolveNamespace(String qname, Node xml) 

Method Source Code

//package com.java2s;
/* //from w  ww  .  j a va2s. c o  m
 * 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.
 *
 */

import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

public class Main {
    /**
     * 
     * Standard prefix for XML namespace attributes.
     * 
     */
    public static final String XMLNS_PREFIX = "xmlns";

    /**
     * 
     * Parses the prefix from the given qualified name and finds the first 
     * XML namespace declaration that maps that prefix to a namespace URI. 
     * If there is no prefix, the method searches for the 
     * <em>targetNamespace</em> attribute instead. The search starts with the 
     * give Node and moves up the XML Document until a match is found or the 
     * root of the Document is reached. 
     *
     * @param qname
     *        The qualified name whose prefix is searched for.
     * 
     * @param xml
     *        The Node from which namespace resolution will start.
     * 
     * @return The namespace URI that the QName's prefix is associated 
     *         with. The method returns null if no match is found.
     *
     */
    public static String resolveNamespace(String qname, Node xml) {
        int colonIndex = qname.indexOf(':');

        //
        // if no prefix is provided, we look for the target NS
        //
        String attributeName = XMLNS_PREFIX;

        if (colonIndex >= 0) {
            String prefix = qname.substring(0, colonIndex);
            attributeName += ':' + prefix;
        }

        //
        // go up the tree until we find something (or hit the root)
        //
        return searchParentNamespaces(xml, attributeName);
    }

    /**
     * 
     * Traverses up the XML Document tree looking for XML an namespace 
     * declaration that matches the given attribute.
     *
     * @param xml
     *        The Node to start the search from. The search will move up 
     *        through the parents of this Node.
     * 
     * @param attribute
     *        The name of the XML namespace attribute to search for. This 
     *        should be <em>xmlns:prefix</em> or <em>targetNamespace</em>.
     * 
     * @return The first value found for the given attribute. The method 
     *         returns null if the attribute is not found in the given Node 
     *         or its parents.
     *
     */
    private static String searchParentNamespaces(Node xml, String attribute) {
        Node next = xml;
        String uri = null;

        //
        // go up the tree until we find a matching prefix/URI (or hit the top)
        //
        while (next != null && (uri == null || uri.length() == 0)) {
            if (next.getNodeType() == Node.ELEMENT_NODE)
                uri = ((Element) next).getAttribute(attribute);

            next = next.getParentNode();
        }

        //
        // DOM returns empty strings for non-existent attributes - we want null
        //
        if (uri != null && uri.length() == 0)
            uri = null;

        return uri;
    }

    public static String getAttribute(Element xml, QName qname) {
        String uri = qname.getNamespaceURI();
        String name = qname.getLocalPart();
        String value = xml.getAttributeNS(uri, name);

        if (value != null && value.length() == 0)
            return null;

        return value;
    }
}

Related

  1. nodeToQName(Node node)
  2. parseXml(final Node node, final Map properties)
  3. printPath(List path)
  4. read(Node node, String expression, QName returnType)
  5. readTextElement(XMLEventReader reader, QName elemName)
  6. resolveQName(final Element el, final String qualifiedName)
  7. resolveQName(String qNameWithPrefix, Element element)
  8. search(List list, Element baseElement, QName nodeName, boolean recursive)
  9. serializeJavaToXml(QName qname, Serializable value)