private static Optional<Element> mostSpecificSibling(Element tag, String siblingName) {
        return Stream.iterate(tag, Objects::nonNull, Node::getParentNode)
                     .filter(Element.class::isInstance)
                     .map(Element.class::cast)
                     .map(element -> XML.getChild(element, siblingName))
                     .filter(Objects::nonNull)
                     .findFirst();
    }
Returns the first encountered sibling with the given name, or sibling of parent, or sibling of grandparent, etc..
config-model-api/src/main/java/com/yahoo/config/application/api/xml/DeploymentSpecXmlReader.java