Java tutorial
//package com.java2s; /* * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import org.w3c.dom.Attr; import org.w3c.dom.Node; public class Main { /** * Returns true if the descendantOrSelf is on the descendant-or-self axis * of the context node. * * @param ctx * @param descendantOrSelf * @return true if the node is descendant */ static public boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) { if (ctx == descendantOrSelf) { return true; } Node parent = descendantOrSelf; while (true) { if (parent == null) { return false; } if (parent == ctx) { return true; } if (parent.getNodeType() == Node.ATTRIBUTE_NODE) { parent = ((Attr) parent).getOwnerElement(); } else { parent = parent.getParentNode(); } } } }