Cheap Web Hosting for Developers

PHP, MySQL, Java, Unix Cheap Web Hosting

PHP and XML CHAPTER 5

Filed under: PHP and XML — webmaster @ 16:27

PHP and XML CHAPTER 5 The latest version of PHP, PHP 5, introduces several new features and enhancements to the PHP language. PHP 5 introduced a new object model, exceptions, and new database support such as MySQLi and SQLite, and it makes major strides in the areas of XML and Web services. This chapter will introduce you to the new XML-based extensions, their founding library, and the basic functionality common to the PHP 5 XML extensions. Introducing XML in PHP 5 Native XML support in PHP 4 was limited to certain basic technologies. The xml extension supported SAX, the domxml extension provided tree support as well as some XSLT support, and the xslt extension also provided XSLT support. With respect to Web services and data exchange, the wddx extension supported distributed data exchange, and xmlrpc supported XML-based remote procedure calls. Although this seems like a decent list of technologies, a fundamental problem was that each was its own distinct extension using its own underlying library. The extensions just did not work together, and to use all the extensions, you had to install all the necessary libraries. The shortcomings of XML in PHP 4 caused much frustration for those using it. All this XML technology was available, but it would not work together. So, while PHP 5 was still in its early stages of development, a discussion began that would ultimately shape the future of XML in PHP 5. The developers decided to rework and rewrite the XML-based extensions to provide the greatest functionality and flexibility as possible. libxml2 in PHP 5 The central library decided upon for the core of the XML extensions is libxml2, which you can find at http://www.xmlsoft.org. This library supports many of XML-related standards, including the XML, Namespaces, XML Schemas, Relax NG, XPath, and XInclude specifications just to name a few. It was chosen for its vast XML support, which means additional technologies can be implemented in PHP, and it is one of the fastest parsers; also, it is actively maintained and widely used. Its sibling, the libxslt library, which is dependent upon libxml2 and also located at http://www.xmlsoft.org, handles XSL within PHP 5. Both of these libraries, being actively maintained, continue to evolve by providing fixes to bugs and enhanced feature sets. To provide the best XML support possible, it is sometimes necessary to require newer versions of these two libraries in order to build PHP. Such is the case with PHP 5.1. The current minimum requirements for libxml2 and libxslt within PHP

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

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE

Filed under: PHP and XML — webmaster @ 05:27

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE 161 sum(for $x in /store/* return if($x/@qty > 25) then $x/@qty * $x/price else 0) You could, of course, have performed this in a much simpler manner: sum(for $x in /store/*[@qty > 25] return $x/@qty * $x/price) The last portion of XPath 2.0 I will cover is quantified expressions. These expressions allow a test against a sequence and return TRUE or FALSE depending upon the quantifier used and whether every item in the sequence evaluates to TRUE or only some do. For example, to test whether every item in the store has a price of 12.99, you could use the following expression: every $x in /store/*/price satisfies $x = 12.99 This expression returns FALSE. The price for the items varies, and only one item has a price of 12.99. You could modify the expression using the some quantifier, which returns TRUE if any of the priceelements have a value of 12.99: some $x in /store/*/price satisfies $x = 12.99 This expression returns TRUE, because a book element exists that has a price equal to 12.99. As you can see, XPath 2.0 is extremely more powerful than XPath 1.0. This brief introduction has only touched the surface of what is contained within XPath 2.0. Many additional functions and keywords perform tasks such as casting, instance-of checking, and schema data typing. In time, these technologies may be available for use with libxml and PHP, but as I have mentioned, there is currently no planned support. By the time you are reading this, things may have changed, but unless the specifications become recommendations soon, I highly doubt it. Conclusion The primary focus of this chapter was on XPath 1.0, XPointer, and XInclude. The material presented should give you enough information about the concepts and actual use of these technologies to utilize them in PHP. Future chapters will build upon what you have learned here and provide you with ways to use this information in the PHP 5 programming environment. You also learned about XPath 2.0 in this chapter. Although PHP doesn t support XPath 2.0, XQuery, or XSLT 2.0, you may encounter an extension at a future date that uses one of these technologies. Using everything you have learned to this point, it is time to begin exploring how to use XML in PHP 5. The next chapter will introduce you to some functionality that is common to the XML-based extensions in PHP.

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

PHP, MySQL, Java, Unix Cheap Web Hosting

160 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND

Filed under: PHP and XML — webmaster @ 18:26

160 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE Of Mice and Men 9.99 2002-10-01 2.99 Using XPath 1.0, you can retrieve all bookand magazine elements with the following: /store/*[self::book or self::magazine] This query would return all the book and magazine elements in document order, which means you would have a node set that contained a book element, a magazine element, a book element, and finally a magazine element. Under XPath 2.0, you could modify the query to retrieve all book elements followed by all magazine elements, followed again by all bookelements. This type of query is not possible under XPath 1.0, because a node set could never contain the same element more than once. For example: (/store/book, /store/magazine, /store/book) This expression is a sequence, where a comma separates each query. The first query retrieves all bookelements, the second query retrieves all magazine elements, and the last query retrieves all book elements again. The result would be a sequence containing the nodes in the order just detailed. As you saw when performing calculations using XPath 1.0, you had no way to generate the total value of inventory on hand. This is now possible using XPath 2.0. Sequences are iterable. It is similar to being able to perform a foreach in PHP: for $x in /store/* return $x/@qty * $x/price This expression, after every iteration has been performed, will return a sequence containing the value of qty*price for each element in the store. The sequence returned would be (324.75, 224.25, 249.75, 14.95). You could then use this sequence within the sumfunction. The sum function in XPath 2.0 takes a sequence, not a node set: sum(for $x in /store/* return $x/@qty * $x/price) The end result would be 833.7, which is the value of the inventory on hand within the store. Another nice addition is if/then/else. The specification s example looks like this: if ($widget1/unit-cost < $widget2/unit-cost) then $widget1 else $widget2 If my interpretation is correct, then you could calculate the total value on hand for items with a quantity greater than 25 with the following:

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

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE

Filed under: PHP and XML — webmaster @ 08:36

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE 159 The value other does not offer any direction for presenting the resource content but indicates to an XLink application that it should look for other markup for possible instructions. The last value, none, is similar to other. It offers no direction for presenting the resource and means that no other markup may exist to offer direction for an application. XLink Summary This has been an extremely brief look at the XLink technology. It has been around for quite a while, and some people think it will revolutionize Internet browsing. XLink has been a recommended specification for more than four years now, and I don t know about you, but I personally don t have a browser that supports it yet. As far as applications using XLink, I haven t yet come across any, although they must exist. If you are interested in further information on XLink and features not covered here, I suggest you read the specification at http://www.w3.org/TR/xlink/. Introducing XQuery, XPath 2.0, and XSLT 2.0 XPath 2.0 is the new generation of XPath. It serves as the foundation for XQuery and XSLT 2.0. These technologies are still in the working draft phase from the W3C. You can find the specifications at http://www.w3.org/TR/xpath20/, http://www.w3.org/XML/Query, and http:// www.w3.org/TR/xslt20/. This section will introduce you to XPath 2.0. XQuery is almost synonymous with XPath 2.0 at this point, but XSLT 2.0 is out of the scope of this book. Although some of the larger database vendors support XQuery, PHP 5 and libxml do not support these technologies natively at this time (and there is currently no planned support). You may find, however, third-party extensions providing support for these technologies, possibly an extension for a database. For these reasons, I ll present only a brief introduction to XPath 2.0. Like XPath 1.0, XPath 2.0 serves to address nodes within an XML tree. It is meant to be used within a host language, such as XQuery and XSLT 2.0, and not as a stand-alone language. A background on XPath 2.0 should suffice in the event you ever encounter XQuery or XSLT 2.0. XPath 2.0 contains the same node types as 1.0, though the terminology for a root node has changed to document node. XPath 2.0 uses the concept of a sequence. Everything is a sequence, including numbers and strings. For example, a single number would be a sequence with a single number, and a string would be considered a sequence with a single string. A node would be a sequence containing one node. In terms of XPath 1.0, a node set would be a sequence of nodes. Listing 4-9 shows a simplified version of the store document from Listing 4-6. Listing 4-9. Simplified Store Document Grapes of Wrath 12.99 2005-11-01 2.99

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

PHP, MySQL, Java, Unix Cheap Web Hosting

158 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND

Filed under: PHP and XML — webmaster @ 22:03

158 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE Listing 4-8. XML Document Using XLink XPath 1.0 Specification XLink Specification Within the document, you should first notice the declaration of the XLink namespace associated with the xlink prefix. The description elements within this document are using xlink attributes, which define link behavior. Many more XLink attributes exist than the ones used here, but those are out of the scope of this book. The following sections will only briefly cover the type, href, and show attributes. type Attribute The type attribute specifies the type of link the element represents. This attribute is mandatory for an element using xlink. The possible values for this attribute are simple, extended, locator, arc, resource, title, and none. The only two values I ll explain here are simple and none, because the remaining values require much more in-depth knowledge of XLink than that provided in this chapter. Using the value none, the element has no XLink meaning. All xlink attributes are skipped, and the element is processed as a normal XML element. The value simple represents a simple link similar to an HTML anchor tag. The remaining values offer more extended functionality. href Attribute The href attribute provides the location for an XLink application to find the remote resource. Its value is a URI, and it works similarly to an hreftag on an HTML anchor element. show Attribute The show attribute indicates where the link should be opened for presentation. Its value may be one of new, replace, embed, other, or none. This attribute is similar to the target attribute for an anchor tag but provides some additional values. The value of new will open the resource in a new window or frame. This is equivalent to a target attribute with the value _blank. The value replace, which is also the default value when not set, will replace the current window or frame with the content. Its HTML target equivalent is _self. The value embedwill embed the contents of the resource within the document. This value is similar to using an image tag, IMG, in HTML.

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

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE

Filed under: PHP and XML — webmaster @ 11:52

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE 157 As expected, the element is not found, resulting in an error and the following document: Element not found XInclude Summary XInclude can be a useful technology to employ. Documents can be smaller in size as well as reused. You can overcome many of the issues with external entities using this method as well as the added ability to fall back to another case in the event of a failure. You can now handle fatal errors, preventing the complete stoppage of processing. In addition, you don t have to load external resources during initial parsing. If the resources are not needed at the time, the xincludes do not need to be processed, which not only keeps the document smaller but also reduces the processing time. Examining the Future of XML Some new technologies are on the horizon in the XML realm. Though not yet standards, these technologies are already being used in many commercialized products. The following sections cover these technologies; I ll also provide an overview of XLink, which although not a new technology is one that does not have as widespread use or support as the technologies already covered. Introducing XLink Although XLink has been a W3C standard for many years now since June 2001 (http:// www.w3.org/TR/xlink) this chapter will not provide an in-depth examination of this technology. It is primarily a UI-based technology, and currently neither PHP nor libxml has native support for XLink. You can create documents containing XLink elements by using extensions such as the DOM extension (which will be covered in Chapter 6), but no XLink processing abilities are offered. You may find some newer browsers beginning to support XLink, but unless you are within a controlled environment, it is not recommended to use XLink for a public site. XLink is for creating and describing links between resources. In terms of HTML, it would be the equivalent to the anchor tag, , on steroids. XLink lives within the http://www.w3.org/1999/xlink namespace. For the purposes of this section, the prefix xlink will be associated with this namespace. XLink allows any element to become a link. This is a big difference from HTML, where the only link is the anchor element. Listing 4-8 illustrates a sample document using XLink.

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

PHP, MySQL, Java, Unix Cheap Web Hosting

156 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND

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

156 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE error. To prevent this from happening, the document could add an xi:fallback element to handle an unexpected case: External Resource Problem Processing this document results in the following: External Resource Problem The contents of xi:fallback were added to the document including the whitespace, such as the line feeds. It is also possible to replace an error condition with no content. You do this simply using an empty xi:fallbackelement: The resulting document in this case is as follows: The academic element still contains the insignificant whitespace from the base document, which is why a blank line appears in the output. You can also perform error handling in cases where an XPointer expression may fail. Using the xpointer attribute, the courseelement identified by the ID c6is to be selected from the document. Looking at the document in Listing 4-7, you already know that no element with this ID exists and expect it to fail: Element not found

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

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE

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

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE 155 The function id() takes a string argument, which must be surrounded by quotes. This appears within an attribute whose value is enclosed in double quotes. For this reason, I used single quotes to encapsulate the string c1. When the attribute value is enclosed by single quotes, the string needs to be encapsulated by double quotes, like xpointer=’xpointer(id(”c1″))’. This returns the courseelement identified by the ID c1from the courses.xmldocument. When included, the resulting document looks like this: Introduction to Languages Including documents, text, and fragments is a straightforward and simple process. All that is required is the addition of an include element, which resides in the http://www.w3.org/ 2001/XInclude namespace, and the location of the resource to be included. Within this section you have come to know this as the xi:include element. XInclude also offers a form of error handling, which is covered next, in the event you encounter a problem with the xinclude. xi:fallback Sometimes an XInclude may fail. It could be because of a problem accessing the remote resource or a possibly invalid selection of data. Normally this would cause an error in processing. XInclude offers a way to handle this and use other functionality in the event of an error. You do this using the xi:fallback element. The fallback element is referenced here using xi:fallback. It falls under the same rules as the xi:includeelement in respect to the namespace. It must reside within the http://www.w3.org/ 2001/XIncludenamespace, which for this chapter has been associated with the xiprefix. This element lives as a child of the xi:includeelement and has no attributes. When an error occurs from the xi:includeelement, the contents of the xi:fallbackelement are used for replacement. Sometimes a network may be unavailable, or an Internet connection goes down. It is also possible that the filename of the remote resource was mistyped in the xi:xincludehref attribute. Each of these would cause the include to fail. Take the case of an invalid href: This href is pointing to coursesBAD.xml, which is a file that does not exist. Processing the xinclude will result in at a minimum a parser warning and possibly an unrecoverable parser

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

PHP, MySQL, Java, Unix Cheap Web Hosting

154 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND

Filed under: PHP and XML — webmaster @ 02:00

154 CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE Introduction to Languages Introduction to French I used the value xml for the parseattribute in this case, so the resource was processed and included as XML. You could also include the resource as text, which will not parse but will escape characters: . Processing the XInclude this time produces something along these lines: <?xml version=”1.0″ ?> <courses> <course xml:id=”c1″> <title>Basic Languages</title> <description>Introduction to Languages</description> </course> <course xml:id=”c2″> <title>French I</title> <description>Introduction to French</description> </course> </courses> Even the XML declaration from the courses.xml file is included this time. XInclude was instructed by the parse attribute not to process the resource as XML but to include it as text. The XML declaration has no meaning as plain text and is added to the document. You most likely have noticed that all characters have also been escaped, including much of the whitespace. If you notice the attributes within the courses.xml file for the course elements, they are defined as xml:id attributes, which automatically convert the attributes to type ID. Using the xpointer attribute with the xi:include element, you can select a single course with the ID of the element:

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

PHP, MySQL, Java, Unix Cheap Web Hosting

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE

Filed under: PHP and XML — webmaster @ 14:47

CHAPTER 4 XPATH, XPOINTER, XINCLUDE, AND THE FUTURE 153 parser The parserattribute specifies how the included resource should be parsed. The possible values are xml and text. When omitted, the default value of xmlis used to parse the include. The value xmlindicates that the resource should be included as parsed XML and merged into the document. The value textindicates that the resource should be included as text content. When including text, escaping will be performed on the contents of the resource to ensure proper text content. For instance, if an XML document were included using text parsing, characters such as would be included using their escaped values, < and >. xpointer This attribute specifies an XPointer expression to be evaluated on the included document. This will allow the include to limit or specify portions of the external xml resource to include. The xpointer attribute is valid only when the parser attribute value is xml, either through omission or explicitly set. Using the xpointer attribute when the parser value is text will result in an error. When the xpointerattribute is omitted, the hrefattribute must be present. encoding The encoding attribute specifies the encoding of a text resource. It is applicable only when the parse attribute is set to text. When parsing XML, the encoding is handled through the normal XML encoding methods. There is no built-in mechanism to specify encoding on non-XML resources, so you can set an encoding name, as defined by the acceptable XML encoding names, as the value of this attribute for this purpose. accept The accept attribute is used for content negotiation while retrieving the resource. When fetching a resource through HTTP, the value of this attribute is added to the HTTP request as an Accept header. accept-language This attribute is also used for content negotiation. Similar to the accept attribute, the value of the accept-languageattribute is added to the HTTP request as an Accept-Language header. Using xi:include The xi:include element is easy to add to a document. Using the external file courses.xml in Listing 4-7, you can construct a document that can include the contents of that file just as if the remote document were contained within the base document: Processing the XInclude within this document results in the following output:

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

Next Page »

Powered by Cheap Web Hosting