218 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM)
218 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM) With this new method, return values can be of type Boolean, Integer, String, Null, or DOMNodeList. It all depends upon the expression used. For instance, an expression could perform a calculation on the year element, such as adding one year and returning the numeric value: $newyear = $domxpath->evaluate(”number(/book/bookinfo/copyright/year) + 1″); var_dump($newyear); You can use this method, also being able to return a DOMNodeList, as a replacement for the query()method. For backward compatibility, query() was left untouched, returning only a DOMNodeList, with support for new additional types added to the evaluate() method: $list = $domxpath->evaluate(”/book/bookinfo/author”); $author = $list->item(0); print $author->nodeName.”n”; This code is almost identical to the code using the query() method, but it uses the evaluate() method. Examining the $authorvariable, you will see it is identical to the $author variable previously returned from the query()method. Using XPath and Namespaces In Chapter 4, I showed the functionality for dealing with namespaces in XPath. I also pointed out the problem with default namespaces. Without prefixes, it is harder to differentiate or write expressions based on namespaces. You can use the helper method, registerNamespace(), on a DOMXPath object to associate a prefix with a namespace, which can then be used in an expression. In Chapter 4, you saw a document containing many different bookelements some in no namespace, some prefixed and within a namespace, and some in a default namespace. One such book element within the default namespace looks like this:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services