Exemplo n.º 1
0
 public function testAddCustomPrefix()
 {
     $book = new fXML($this->ns_xml);
     $book->addCustomPrefix('_', 'http://will');
     $pages = $book->xpath('foo:chapter/_:page');
     $this->assertEquals(2, count($pages));
     $this->assertEquals(TRUE, $pages[0] instanceof fXML);
     $this->assertEquals(TRUE, $pages[1] instanceof fXML);
 }
Exemplo n.º 2
0
 /**
  * Executes an XPath query on the current element, returning an array of matching elements
  * 
  * @param  string  $path        The XPath path to query
  * @param  boolean $first_only  If only the first match should be returned
  * @return array|string|fXML  An array of matching elements, or a string or fXML object if `$first_only` is `TRUE`
  */
 public function xpath($path, $first_only = FALSE)
 {
     $result = $this->query($path);
     if ($first_only) {
         if (!$result->length) {
             return NULL;
         }
         $result = array($result->item(0));
     } else {
         if (!$result->length) {
             return array();
         }
     }
     $keys_to_remove = array();
     $output = array();
     foreach ($result as $element) {
         if ($element instanceof DOMElement) {
             $child = new fXML($element);
             $child->__custom_prefixes = $this->__custom_prefixes;
             if ($child->__dom->namespaceURI && $child->__dom->prefix == '') {
                 $child->addCustomPrefix('__', $child->__dom->namespaceURI);
             }
             $output[] = $child;
         } elseif ($element instanceof DOMCharacterData) {
             $output[] = $element->data;
         } elseif ($element instanceof DOMAttr) {
             $key = $element->name;
             if ($element->prefix) {
                 $key = $element->prefix . ':' . $key;
             }
             // We will create an attrname and attrname[0] key for each
             // attribute and if more than one is found we remove the
             // key attrname. If only one is found we remove attrname[0].
             $key_1 = $key . '[1]';
             if (isset($output[$key_1])) {
                 $i = 1;
                 while (isset($output[$key . '[' . $i . ']'])) {
                     $i++;
                 }
                 // This removes the key without the array index if more than one was found
                 unset($output[$key]);
                 unset($keys_to_remove[$key_1]);
                 $key = $key . '[' . $i . ']';
             } else {
                 $output[$key_1] = $element->nodeValue;
                 $keys_to_remove[$key_1] = TRUE;
             }
             $output[$key] = $element->nodeValue;
         }
     }
     foreach ($keys_to_remove as $key => $trash) {
         unset($output[$key]);
     }
     if ($first_only) {
         return current($output);
     }
     return $output;
 }