}
  void doApply(Stylesheet stylesheet, QName mode,               Node context, int pos, int len,               Node parent, Node nextSibling)    throws TransformerException  {    Object ret = select.evaluate(context, pos, len);    Document doc = (parent instanceof Document) ? (Document) parent :      parent.getOwnerDocument();    if (ret instanceof Collection)      {        Collection ns = (Collection) ret;        List list = new ArrayList(ns);        Collections.sort(list, documentOrderComparator);        for (Iterator i = list.iterator(); i.hasNext(); )          {            Node src = (Node) i.next();            short nodeType = src.getNodeType();            if (nodeType == Node.DOCUMENT_NODE)              {                // Use document element                src = ((Document) src).getDocumentElement();                if (src == null)                  {                    continue;                  }                nodeType = Node.ELEMENT_NODE;              }            else if (nodeType == Node.ATTRIBUTE_NODE)              {                if (parent.getFirstChild() != null)                  {                    // Ignore attempt to add attribute after children                    continue;                  }              }            if (parent.getNodeType() == Node.ATTRIBUTE_NODE &&                nodeType != Node.TEXT_NODE &&                nodeType != Node.ENTITY_REFERENCE_NODE)              {                // Ignore                continue;              }            Node node = src.cloneNode(true);            node = doc.adoptNode(node);            if (nodeType == Node.ATTRIBUTE_NODE)              {                NamedNodeMap attrs = parent.getAttributes();                if (attrs != null)                  {                    attrs.setNamedItemNS(node);                  }              }            else              {                if (nextSibling != null)                  {                    parent.insertBefore(node, nextSibling);                  }                else                  {                    parent.appendChild(node);                  }              }          }      }    else      {        String value = Expr._string(context, ret);        if (value != null && value.length() > 0)          {            Text textNode = doc.createTextNode(value);            if (nextSibling != null)              {                parent.insertBefore(textNode, nextSibling);              }            else              {                parent.appendChild(textNode);              }          }      }    // copy-of doesn't process children    if (next != null)      {        next.apply(stylesheet, mode,                   context, pos, len,                   parent, nextSibling);      }  }