code: public ElemTemplate getTemplate(XPathContext xctxt,
                                  int targetNode,
                                  QName mode,
                                  boolean quietConflictWarnings,
                                  DTM dtm)
              throws TransformerException
    {
  
      TemplateSubPatternAssociation head = getHead(xctxt, targetNode, dtm);
  
      if (null != head)
      {
        // XSLT functions, such as xsl:key, need to be able to get to 
        // current ElemTemplateElement via a cast to the prefix resolver.
        // Setting this fixes bug idkey03.
        xctxt.pushNamespaceContextNull();
        xctxt.pushCurrentNodeAndExpression(targetNode, targetNode);
        try
        {
          do
          {
            ElemTemplate template = head.getTemplate();        
            xctxt.setNamespaceContext(template);
            
            if ((head.m_stepPattern.execute(xctxt, targetNode) != NodeTest.SCORE_NONE)
                    && head.matchMode(mode))
            {
              if (quietConflictWarnings)
                checkConflicts(head, xctxt, targetNode, mode);
  
              return template;
            }
          }
          while (null != (head = head.getNext()));
        }
        finally
        {
          xctxt.popCurrentNodeAndExpression();
          xctxt.popNamespaceContext();
        }
      }
  
      return null;
    }
docs: Given a target element, find the template that best
  matches in the given XSL document, according
  to the rules specified in the xsl draft.
  
  @param xctxt
  @param targetNode
  @param mode A string indicating the display mode.
  @param quietConflictWarnings
  @return Rule that best matches targetElem.
  @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  the error condition is severe enough to halt processing.
  
  @throws TransformerException