Generate Atom feeds from your data

class atom1 extends DOMDocument { private $ns;   public function __construct($title, $href, $name, $id) { parent::__construct(); $this->formatOutput = true;   $this->ns = ‘http://www.w3.org/2005/Atom’;   $root = $this->appendChild($this->createElementNS($this->ns, ‘feed’));   $root->appendChild($this->createElementNS($this->ns, ‘title’, $title)); $link = $root->appendChild($this->createElementNS($this->ns, ‘link’)); $link->setAttribute(’href’, $href); $root->appendChild($this->createElementNS($this->ns, ‘updated’, date(’Y-m-d\\TH:i:sP’))); $author = $root->appendChild($this->createElementNS($this->ns, ‘author’)); $author->appendChild($this->createElementNS($this->ns, ‘name’, $name)); $root->appendChild($this->createElementNS($this->ns, ‘id’, $id)); }   public function […]
Continue reading…

Enjoyed this post? Share it!

 
 

Generate RSS feeds from your data

<?php class rss2 extends DOMDocument { private $channel;   public function __construct($title, $link, $description) { parent::__construct(); $this->formatOutput = true;   $root = $this->appendChild($this->createElement(’rss’)); $root->setAttribute(’version’, ‘2.0’);   $channel= $root->appendChild($this->createElement(’channel’));   $channel->appendChild($this->createElement(’title’, $title)); $channel->appendChild($this->createElement(’link’, $link)); $channel->appendChild($this->createElement(’description’, $description));   $this->channel = $channel; }   public function addItem($title, $link, $description) { $item = $this->createElement(’item’); $item->appendChild($this->createElement(’title’, $title)); $item->appendChild($this->createElement(’link’, $link)); $item->appendChild($this->createElement(’description’, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Reading RSS and Atom Feeds using MagpieRSS parser

Using the MagpieRSS parser here’s an example that reads the RSS feed. <?php require ‘rss_fetch.inc’;   $feed = ‘http://feeds.feedburner.com/w3mentor?format=rss’;   $rss = fetch_rss( $feed );   print "<ul>\n"; foreach ($rss->items as $item) { print ‘<li><a href="’ . $item[’link’] . ‘">’ . $item[’title’] . "</a></li>\n"; } print "</ul>\n"; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Validating XML String using PHP

With XML in a string, call DOMDocument::schemaValidateSource() or DOMDocument::relaxNGValidateSource(): $xml = ‘<person><firstname>Raja</firstname></person>’; $schema = ‘address.xsd’; $ab = new DOMDocument $ab->load($file);   if ($ab->&gt;schemaValidateSource($schema)) { print "XML is valid.\n"; } else { print "XML is invalid.\n"; }
Continue reading…

Enjoyed this post? Share it!

 
 

Call user defined functions from XSL

function replace_email($nodes) { return preg_replace(’/([^@\s]+)@([-a-z0-9]+\.)+[a-z]{2,}/is’, ‘$1@…’, $nodes[0]->nodeValue); }   $dom = new DOMDocument; $dom->load(’addresses.xml’); $xsl = new DOMDocument $xsl->load(’stylesheet.xsl’);   $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsl); $xslt->registerPhpFunctions(); print $xslt->transformToXML($dom); Stylesheet.xsl <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xsl:extension-element-prefixes="php">   <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>     <xsl:template match="/addresses/person/email"> <xsl:copy> <xsl:value-of select="php:function(‘replace_email’, node())" /> </xsl:copy> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Calling PHP Functions from XSLT Stylesheets

Invoke the XSLTProcessor::registerPHPFunctions( ) method to enable this functionality: $xslt = new XSLTProcessor(); $xslt->registerPHPFunctions(); And use the function( ) or functionString( ) function within your stylesheet: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xsl:extension-element-prefixes="php">   <xsl:template match="/"> <xsl:value-of select="php:function(‘strftime’, ‘%c’)" /> </xsl:template>   </xsl:stylesheet>
Continue reading…

Enjoyed this post? Share it!

 
 

Set XSLT Parameters from PHP

Use the XSLTProcessor::setParameter( ) method: The first argument is the namespace and can be null $uid = ‘123’;   $dom = new DOMDocument $dom->load(’addresses.xml’); $xsl = new DOMDocument $xsl->load(’stylesheet.xsl’);   $xslt = new XSLTProcessor(); $xslt->importStylesheet($xsl); $xslt->setParameter(NULL, ‘user_id’, $uid); print $xslt->transformToXML($dom); stylesheet.xsl: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Check if XSLT Transformation failed

// Load XSL template $xsl = newDOMDocument; $xsl->load(’stylesheet.xsl’);   // Create new XSLTProcessor $xslt = new XSLTProcessor(); // Load stylesheet $xslt->importStylesheet($xsl);   // Load XML input file $xml = new DOMDocument; $xml->load(’data.xml’);   // Transform to string $results = $xslt->transformToXML($xml);   // Transform to a file $results = $xslt->transformToURI($xml, ‘results.txt’);   // Transform to DOM […]
Continue reading…

Enjoyed this post? Share it!

 
 

Transforming XML with XSLT

Use PHP’s XSLT extension: // Load XSL template $xsl = newDOMDocument; $xsl->load(’stylesheet.xsl’);   // Create new XSLTProcessor $xslt = new XSLTProcessor(); // Load stylesheet $xslt->importStylesheet($xsl);   // Load XML input file $xml = new DOMDocument; $xml->load(’data.xml’);   // Transform to string $results = $xslt->transformToXML($xml);   // Transform to a file $results = $xslt->transformToURI($xml, ‘results.txt’);   […]
Continue reading…

Enjoyed this post? Share it!

 
 

Using XPath with SimpleXML

$s = simplexml_load_file(’addresses.xml’); $people = $s->xpath(’/addresses/person’);   foreach($people as $p) { list($firstname) = $p->xpath(’firstname’); list($lastname) = $p->xpath(’lastname’);   print "$firstname $lastname\n"; } Input XML: <?xml version="1.0"?> <addresses> <person> <name>Ramesh babu</name> <firstname>Ramesh</firstname> <lastname>babu</lastname> <email>Ram@w3mauthor.com</email> </person> <person> <name>Jolld babu</name> <firstname>jolld</firstname> <lastname>babu</lastname> <email>Jolld@w3mauthor</email> </person> </addresses>
Continue reading…

Enjoyed this post? Share it!

 
 

Using XPath with DOM

$dom = new DOMDocument; $dom->load(’addresses.xml’); $xpath = new DOMXPath($dom); $person = $xpath->query(’/addresses/person’);   foreach ($person as $p) { $fn = $xpath->query(’firstname’, $p); $firstname = $fn->item(0)->firstChild->nodeValue;   $ln = $xpath->query(’lastname’, $p); $lastname = $ln->item(0)->firstChild->nodeValue;   print "$firstname $lastname\n"; } Input XML: <?xml version="1.0"?> <addresses> <person> <name>Ramesh babu</name> <firstname>Ramesh</firstname> <lastname>babu</lastname> <email>Ram@w3mauthor.com</email> </person> <person> <name>Jolld babu</name> <firstname>jolld</firstname> <lastname>babu</lastname> […]
Continue reading…

Enjoyed this post? Share it!

 
 

Get Information Using XPath in DOM

<?php $dom = new DOMDocument; $dom->load(’addresses.xml’); $xpath = new DOMXPath($dom); $email = $xpath->query(’/addresses/person/email’);   foreach ($emails as $email) { // do something with $email } ?> Input XML: <?xml version="1.0"?> <addresses> <person> <name>Ramesh babu</name> <email>Ram@w3mauthor.com</email> </person> <person> <name>Jolld</name> <email>Jolld@w3mauthor</email> </person> </addresses>
Continue reading…

Enjoyed this post? Share it!

 
 

Get Information Using XPath in simpleXML

XPath is available in SimpleXML: <?php $s = simplexml_load_file(’addresses.xml’); $emails = $s->xpath(’/addresses/person/email’);   foreach ($emails as $email) { // do something with $email } ?> Input XML: <?xml version="1.0"?> <addresses> <person> <name>Ramesh babu</name> <email>Ram@w3mauthor.com</email> </person> <person> <name>Jolld</name> <email>Jolld@w3mauthor</email> </person> </addresses>
Continue reading…

Enjoyed this post? Share it!

 
 

XMLReader node type values

Node type Description XMLReader::NONE No node type XMLReader::ELEMENT Start element XMLReader::ATTRIBUTE Attribute node XMLReader::TEXT Text node XMLReader::CDATA CDATA node XMLReader::ENTITY_REF Entity Reference node XMLReader::ENTITY Entity Declaration node XMLReader::PI Processing Instruction node XMLReader::COMMENT Comment node XMLReader::DOC Document node XMLReader::DOC_TYPE Document Type node XMLReader::DOC_FRAGMENT Document Fragment node XMLReader::NOTATION Notation node XMLReader::WHITESPACE Whitespace node XMLReader::SIGNIFICANT_WHITESPACE Significant Whitespace node […]
Continue reading…

Enjoyed this post? Share it!

 
 

Parsing Large XML Documents using XMLReader

Use the XMLReader extension: open(‘input.xml’); /* Loop through document */ while ($reader->read()) { if ($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == ‘author’) { $reader->read(); print $reader->value . “\n”; } } ?> Input XML: <?xml version="1.0"?> <books> <book> <title>Learn PHP XML</title> <author>w3mauthor</author> <author>w3mauthor1</author> <subject>PHP</subject> </book> <book> <title>Learn Perl</title> <author>w3mauthor</author> <author>w3mauthor1</author> <subject>Perl</subject> </book> </books>
Continue reading…

Enjoyed this post? Share it!

 
 

Printing all nodes using getElementsByTagname in DOM

// find and print all authors $authors = $dom->getElementsByTagname(’author’);   // loop through author elements foreach ($authors as $author) { // childNodes holds the author values $text_nodes = $author->childNodes;   foreach ($text_nodes as $text) { print $text->nodeValue . "\n"; } } Input XML: <?xml version="1.0"?> <books> <book> <title>Learn PHP XML</title> <author>w3mauthor</author> <author>w3mauthor1</author> <subject>PHP</subject> </book> <book> […]
Continue reading…

Enjoyed this post? Share it!