public function toString($applystyles = true)
    {
        // create XsltProcessor if needed
        if ($applystyles && !empty($this->styles)) {
            $proc = self::stylesToProc($this->styles);
            $proc->registerPhpFunctions();
        }

        // source parsing if needed
        switch ($this->sourceType) {
            case self::SOURCE_TYPE_DOM:
                $dom = $this->source;
                break;
            case self::SOURCE_TYPE_SIMPLEXML:
                if (!isset($proc)) {
                    return $this->source->asXML();
                }
                // no break
            default:
                $dom = $this->toDOM(false);
        }

        // if no stylesheets were selected, just return XML string
        if (!isset($proc)) {
            if (!$dom instanceof DomDocument) {
                // when parameter for constructor was DomNode and not domdocument
                return $dom->ownerDocument->saveXML($dom);
            }

            return $dom->saveXML();
        }

        // apply stylesheets and return parsed data as string.
        return (string) $proc->transformToXML($dom);
    }