protected function appendNodeBefore(DOMNode $node, $xquery = null, $nodeNameList = array())
    {
      if(!is_array($nodeNameList))
        throw new InvalidArgumentException(__METHOD__ . '; Nodes should be an array');

      // Determine parent node
      if(!is_null($xquery))
        $parentNode = $this->getNode($xquery);
      else if(!is_null($this->node))
        $parentNode = $this->node;
      else
        $parentNode = $this->document;

      // Init child node (the one we are looking for)
      $childNode = null;

      // Iterate through existing childNodes
      if($parentNode->hasChildNodes())
      {
        // Get child nodes from parent
        $childNodes = $this->convertNodeListToArray($parentNode->childNodes);

        // Reset pointer
        reset($childNodes);

        while(current($childNodes) != null && is_null($childNode))
        {
          if(in_array(current($childNodes)->nodeName, $nodeNameList) || in_array(current($childNodes)->localName, $nodeNameList))
            $childNode = current($childNodes);

          // Next element
          next($childNodes);
        }
      }

      if(!is_null($childNode))
        $parentNode->insertBefore($node, $childNode);
      else
        $parentNode->appendChild($node);
    }