Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

218 CHAPTER 6 DOCUMENT OBJECT MODEL (DOM)

Filed under: PHP and XML — webmaster @ 23:35

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: To Kill a Mockingbird 10.99 2002-03-01 Lee, Harper It tends to be easier when writing expressions to be able to use a prefix when dealing with namespaced nodes. In this case, you can use the registerNamespace() method: $domxpath->registerNamespace(”ec”, ” http://www.example.com/ExternalClassics”); Prior to executing an expression, if the namespace http://www.example.com/ ExternalClassics and the prefix ec are registered with the DOMXPathclass, you can use the prefix within an expression to access namespaced nodes. It is important to understand that when performing a query or evaluation with an XPath object, some automatic namespace registration takes place. Prefixed namespaces within the scope of the context node are automatically registered and can be used within an expression. When a context node is not supplied, prefixed namespaces on the document element are

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Cheap Web Hosting