/** * Retrieves an element or DOMIT_NodeList of elements corresponding to an Xpath-like expression. * @param string The query pattern * @param int If a single node is to be returned (rather than the entire NodeList) the index of that node * @return mixed A NodeList or single node that matches the pattern */ function &getElementsByPath($pattern, $nodeIndex = 0) { require_once DOMIT_INCLUDE_PATH . 'xml_domit_getelementsbypath.php'; $gebp = new DOMIT_GetElementsByPath(); $myResponse =& $gebp->parsePattern($this, $pattern, $nodeIndex); return $myResponse; }
/** * Matches the current path segment against the child nodes of the current context node * @param Object The context node * @param string The pattern * @param int The index of the current path segment */ function &parsePattern(&$node, $pattern, $nodeIndex = 0) { $beginSquareBrackets = strpos($pattern, '['); if ($beginSquareBrackets != 0) { $path = substr($pattern, 0, $beginSquareBrackets); $attrPattern = substr($pattern, strpos($pattern, '@') + 1); $attrPattern = substr($attrPattern, 0, strpos($attrPattern, ')')); $commaIndex = strpos($attrPattern, ','); $key = trim(substr($attrPattern, 0, $commaIndex)); $value = trim(substr($attrPattern, $commaIndex + 1)); $value = substr($value, 1, strlen($value) - 2); $gebp = new DOMIT_GetElementsByPath(); $myResponse =& $gebp->parsePattern($node, $path); $total = $myResponse->getLength(); for ($i = 0; $i < $total; $i++) { $currNode =& $myResponse->item($i); if ($currNode->hasAttribute($key)) { if ($currNode->getAttribute($key) == $value) { $this->nodeList->appendNode($currNode); } } } } if ($nodeIndex == 0) { return $this->nodeList; } else { if ($nodeIndex <= $this->nodeList->getLength()) { return $this->nodeList->item($nodeIndex - 1); } else { $this->nodeList = new DOMIT_NodeList(); return $this->nodeList; } } }