code: public void validate(ElemTemplateElement elem, StylesheetHandler handler)
      throws SAXException
    {
      String msg = "";
      while (elem != null)
      { 
        //System.out.println("elem " + elem);
        if (elem instanceof ElemExsltFuncResult 
            && elem.getNextSiblingElem() != null 
            && !(elem.getNextSiblingElem() instanceof ElemFallback))
        {
          msg = "func:result has an illegal following sibling (only xsl:fallback allowed)";
          handler.error(msg, new SAXException(msg));
        }
        
        if((elem instanceof ElemApplyImport
  	 || elem instanceof ElemApplyTemplates
  	 || elem instanceof ElemAttribute
  	 || elem instanceof ElemCallTemplate
  	 || elem instanceof ElemComment
  	 || elem instanceof ElemCopy
  	 || elem instanceof ElemCopyOf
  	 || elem instanceof ElemElement
  	 || elem instanceof ElemLiteralResult
  	 || elem instanceof ElemNumber
  	 || elem instanceof ElemPI
  	 || elem instanceof ElemText
  	 || elem instanceof ElemTextLiteral
  	 || elem instanceof ElemValueOf)
  	&& !(ancestorIsOk(elem)))
        {
          msg ="misplaced literal result in a func:function container.";
          handler.error(msg, new SAXException(msg));
        }
        ElemTemplateElement nextElem = elem.getFirstChildElem();
        while (nextElem == null)
        {
          nextElem = elem.getNextSiblingElem();
          if (nextElem == null)
            elem = elem.getParentElem();
          if (elem == null || elem instanceof ElemExsltFunction)
            return; // ok
        }  
        elem = nextElem;
      }
    }
docs: Non-recursive traversal of FunctionElement tree based on TreeWalker to verify that
  there are no literal result elements except within a func:result element and that
  the func:result element does not contain any following siblings except xsl:fallback.