Java XPath Expression constructXPathForElement(Element inElem, String xPathRest)

Here you can find the source of constructXPathForElement(Element inElem, String xPathRest)

Description

construct X Path For Element

License

Common Public License

Declaration

public static String constructXPathForElement(Element inElem, String xPathRest) 

Method Source Code

//package com.java2s;
//License from project: Common Public License 

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

public class Main {
    /**/*from ww w . j av a2 s  .  co  m*/
     * Constructs an XPath to an element.
     * 
     * Assumes that attributes named "id" are ID-type attributes.
     * @param inElem
     * @return
     */
    public static String constructXPathForElement(Element inElem) {
        return constructXPathForElement(inElem, "");
    }

    public static String constructXPathForElement(Element inElem, String xPathRest) {
        // Start at the node, walk up its ancestry. 
        // NOTE: We are using qualified names to make sure we have appropriate
        // namespace prefixes. These prefixes are specific to the original
        // document content in which the element exists and can only be
        // reliably resolved in that context.
        String xpathPart = "/" + inElem.getNodeName();
        if (inElem.hasAttribute("id")) {
            xpathPart = "/" + xpathPart + "[@id = '" + inElem.getAttribute("id") + "']";
        } else {
            xpathPart += "[" + countPrecedingSiblingsOfType(inElem) + "]";
            if (inElem.getParentNode() != null) {
                if (inElem.getParentNode().getNodeType() == Node.ELEMENT_NODE) {
                    return constructXPathForElement((Element) (inElem.getParentNode()), xpathPart + xPathRest);
                }
            }
        }
        return xpathPart + xPathRest;
    }

    /**
     * @param inElem
     * @return
     */
    private static int countPrecedingSiblingsOfType(Element inElem) {
        int cnt = 1; // Elements are 1-indexed in XPath
        Node node = inElem.getPreviousSibling();
        String typeName = inElem.getNodeName();
        while (node != null) {
            while (node.getNodeType() != Node.ELEMENT_NODE) {
                node = node.getPreviousSibling();
                if (node == null) {
                    break;
                }
            }
            if (node != null) {
                if (((Element) node).getNodeName().equals(typeName)) {
                    cnt++;
                }
                node = node.getPreviousSibling();
            }
        }
        return cnt;
    }
}

Related

  1. asLong(String expression, Node node)
  2. compile(String expression)
  3. compile(String expression)
  4. compile(String path)
  5. compileXPathExpression(String xPathExpression)
  6. createFactory()
  7. createNewXPath(@Nullable final NamespaceContext aNamespaceContext)
  8. createQueryResourcePropertiesCallBody(String aXqueryExpression)
  9. createReverseCMDIComponentItemMap(String urlToComponent)