Java XPath Create getXPathForElement(Node e, NamespaceContext ctx)

Here you can find the source of getXPathForElement(Node e, NamespaceContext ctx)

Description

get X Path For Element

License

Open Source License

Declaration

public static String getXPathForElement(Node e, NamespaceContext ctx) 

Method Source Code

//package com.java2s;
/**//from   ww  w  . j  a  v a 2s  . c  o m
 * This file belongs to the BPELUnit utility and Eclipse plugin set. See enclosed
 * license file for more information.
 */

import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.NamespaceContext;

import org.w3c.dom.Attr;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getXPathForElement(Node e, NamespaceContext ctx) {
        StringBuffer sb = new StringBuffer();
        List<Node> path = new ArrayList<Node>();

        Node currentNode = e;
        while (currentNode.getParentNode() != currentNode.getOwnerDocument()) {
            path.add(0, currentNode);
            if (currentNode instanceof Attr) {
                Attr a = (Attr) currentNode;
                currentNode = a.getOwnerElement();
            } else {
                currentNode = currentNode.getParentNode();
            }
        }
        path.add(0, currentNode); // We need the root element

        for (Node n : path) {
            sb.append("/");

            if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
                sb.append("@");
            }

            String namespaceURI = n.getNamespaceURI();
            if (namespaceURI != null && !namespaceURI.equals("")) {
                sb.append(ctx.getPrefix(namespaceURI)).append(":");
            }
            sb.append(n.getLocalName());

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                appendElementQualifier(sb, (Element) n);
            }
        }

        return sb.toString();
    }

    /**
     * Append XPath [expr] like /a/b[1] or /a/b[@name='x']
     * @param sb
     * @param n
     */
    private static void appendElementQualifier(StringBuffer sb, Element n) {
        if (n.getParentNode() == n.getOwnerDocument()) {
            return;
        }

        if (n.getAttributes() != null && n.getAttributes().getNamedItem("name") != null) {
            sb.append("[@name='").append(n.getAttributes().getNamedItem("name").getNodeValue()).append("']");
        } else if (getChildrenCount((Element) n.getParentNode(), n.getNamespaceURI(), n.getLocalName()) != 1) {
            sb.append("[").append(getPosition(n)).append("]");
        }
    }

    private static int getChildrenCount(Element parentNode, String namespaceURI, String localName) {
        int counter = 0;

        List<Element> children = getChildElements(parentNode);
        for (Element c : children) {
            if (namespaceURI.equals(c.getNamespaceURI()) && localName.equals(c.getLocalName())) {
                counter++;
            }
        }

        return counter;
    }

    /**
     * Corresponds to the pos() xpath function
     * 
     * @param e
     * @return pos(), also the start index is 1 not 0
     */
    public static int getPosition(Element e) {
        Element parent = (Element) e.getParentNode();
        List<Element> children = getChildElements(parent);
        return children.indexOf(e) + 1;
    }

    public static List<Element> getChildElements(Element element) {
        NodeList childNodes = element.getChildNodes();

        List<Element> elements = new ArrayList<Element>();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n = childNodes.item(i);
            if (n instanceof Element) {
                elements.add((Element) n);
            }
        }
        return elements;
    }
}

Related

  1. getXPathFactory()
  2. getXPathFactory()
  3. getXPathFactory()
  4. getXPathFactory()
  5. getXpathFilter(String xpath, Map namespaceMap)
  6. getXPathForNode(Node node)
  7. getXPathForNode(Node node)
  8. getXPathFromVector(Vector path)
  9. getXPathFromVector(Vector path)