Dealing with XML Namespaces


Many feeds that you will encounter will likely include some form of XML namespacing; you can recognize namespaces by two elements within the document. First, within the declaration of the root element, as seen here for an RSS blog:

 <rss version="2.0"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:admin="http://webns.net/mvcb/"    xmlns:dc="http://purl.org/dc/elements/1.1/"    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"    xmlns:wfw="http://wellformedweb.org/CommentAPI/"    xmlns:content="http://purl.org/rss/1.0/modules/content/"> 

The document declares itself as RSS version 2.0, then proceeds to define each of the XML namespaces (XMLNS) that the document will make use of. In this sample, the namespaces are rdf, admin, dc, slash, wfw, and content, respectively (the portion after the colon).

You will also notice the namespacing within the document itself, because there will be elements containing a colon:

 <dc:language>en</dc:language> 

This is an element within the dc namespace named language, with content of en. This declares the language of the document to be English.

In order to interact with namespaced elements within the document, and through SimpleXML, the namespaces will need to be properly declared in the document and you will need to access it specifically with the children method. For example:

 $dc = $xml->children("http://purl.org/dc/elements/1.1/"); $language = $dc->language 

The first line extracts all of the dc namespaced children of the root node, and the second line accesses the language element.




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net