@Override
  protected boolean extractImpl(XPathExpression expression, Set<XPathExpression> matchingExpressions, List<XmlTag> otherMatches, RefactoringOptions dlg) {
    final XmlAttribute attribute = PsiTreeUtil.getContextOfType(expression, XmlAttribute.class, true);
    assert attribute != null;

    try {
      final String name = dlg.getName();
      final XmlTag rootTag = ((XmlFile)attribute.getParent().getContainingFile()).getRootTag();
      final XmlTag[] templates = rootTag.findSubTags("template", XsltSupport.XSLT_NS);
      final XmlTag insertionPoint = templates.length > 0 ? templates[0] : rootTag.getSubTags()[0];

      final XmlTag parentTag = insertionPoint.getParentTag();
      assert parentTag != null : "Could not locate position to create function at";

      final XmlTag xmlTag = parentTag.createChildTag("function", XsltSupport.XSLT_NS, null, false);
      xmlTag.setAttribute("name", name);

      final XPathType type = ExpectedTypeUtil.mapType(expression, expression.getType());
      xmlTag.setAttribute("as", prefixedName(type, insertionPoint));

      final StringBuilder argList = new StringBuilder();
      final List<XPathVariableReference> references = RefactoringUtil.collectVariableReferences(expression);
      for (XPathVariableReference reference : references) {
        final XPathVariable variable = reference.resolve();
        if (variable instanceof XsltVariable) {
          // don't pass through global parameters and variables
          if (XsltCodeInsightUtil.getTemplateTag(variable, false) != null) {
            final XmlTag param = parentTag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
            param.setAttribute("name", variable.getName());
            if (!variable.getType().isAbstract()) {
              param.setAttribute("as", prefixedName(ExpectedTypeUtil.mapType(expression, variable.getType()), parentTag));
            }
            RefactoringUtil.addParameter(xmlTag, param);

            if (argList.length() > 0) {
              argList.append(", ");
            }
            argList.append("$").append(variable.getName());
          }
        }
      }

      final XmlTag seqTag = parentTag.createChildTag("sequence", XsltSupport.XSLT_NS, null, false);
      seqTag.setAttribute("select", expression.getText());
      xmlTag.add(seqTag);

      // TODO: revisit the formatting
      final PsiElement element = parentTag.addBefore(xmlTag, insertionPoint);
      final ASTNode node1 = parentTag.getNode();
      assert node1 != null;
      final ASTNode node2 = element.getNode();
      assert node2 != null;
      CodeStyleManager.getInstance(xmlTag.getManager().getProject()).reformatNewlyAddedElement(node1, node2);

      final XPathExpression var = XPathChangeUtil.createExpression(expression, name + "(" + argList + ")");
      expression.replace(var);

      return true;
    } catch (IncorrectOperationException e) {
      Logger.getInstance(getClass().getName()).error(e);
      return false;
    }
  }
@Override
  protected boolean extractImpl(XPathExpression expression, Set<XPathExpression> matchingExpressions, List<XmlTag> otherMatches, RefactoringOptions dlg)