TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-22-2010, 10:50 AM   #1 (permalink)
The Visitor
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
mobimad is on a distinguished road
Default How do i Display XML data with Namespace using XSLT Help

Hi Guys, I'm having some real problems using XSLT to display data from a xml file with namespaces. Could anyone advise what i need to do to display the contents of

<ad:advertisement> title, subTitle, displayUrl </ad:advertisement>

and

<item> title, description, link </item>

Here is the XML file, please help, Thankyou, Lucy

Code:
<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
xmlns="http://blogs.law.harvard.edu/tech/rss" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xAL="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0mc"
xmlns:gml="http://www.opengis.net/gml"
xmlns:mc="http://www.mocomm.co.uk/search/responseextensions"
xmlns:ad="http://a9.com/-/opensearch/extensions/advertisement/1.0/"
xmlns:media="http://video.search.yahoo.com/mrss"
xmlns:georss="http://www.georss.org/georss/10"
xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/">

-
<channel>
<link>http://www.mocomm.co.uk</link>
<description>Search results for "wallpapers"</description>
<lastBuildDate>Fri, 10 Sep 2010 13:02:56 +01:00</lastBuildDate>
<category domain="mcrating">general</category>
-
<category domain="mctaxonomy">
personalisation/downloads / wallpapers / wallpapers
</category>
<category domain="mcdisplayad">Personalisation, downloads</category>

<openSearch:totalResults>2656621</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>6</openSearch:itemsPerPage>
<openSearch:Query role="request" searchTerms="wallpapers"/>
<openSearch:Query role="mc:context" searchTerms="wallpapers" mc:token="Uj03YjUxNzI4Mi0wMzY5LTmNi0yNDBkMDIyMDRjYzdQsQT0xTg2"/>
<openSearch:Query role="mc:substitution" searchTerms="free wallpapers"/>
-
<mc:adSource>
<mc:market>UK</mc:market>
<mc:pool>other</mc:pool>
<mc:moreResults>false</mc:moreResults>

</mc:adSource>
-
<ad:advertisement>
<ad:title>Cute Pooch Cleans Phone Screen, Adorable</ad:title>
<ad:subTitle>Pooch Licks Phone.</ad:subTitle>
<ad:displayUrl>moble.mobi</ad:displayUrl>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/A/1
</link>
</ad:advertisement>
-
<ad:advertisement>
<ad:title>Blah Blah Phone</ad:title>
<ad:subTitle> Phonefun stuff.</ad:subTitle>
<ad:displayUrl>mobzz.mobi</ad:displayUrl>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d7878204cc4/A/2
</link>
</ad:advertisement>

<item>
<title>Free Mobile Wallpaper</title>

-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/2
</link>
<description>Download any of these wallpaper backgrounds for...</description>
<mc:resultType>M</mc:resultType>
<mc:displayUrl>wap.gambile.co.uk</mc:displayUrl>
</item>
-
<item>
<title>Top 3 Realtones</title>
-
<link>

http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/3
</link>
<description>... Videos - Video Download Music - Colour...</description>
<mc:resultType>M</mc:resultType>
<mc:displayUrl>wap.jacksa.co.uk</mc:displayUrl>
</item>
-
<item>
<title>WAP Dowloads</title>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/4
</link>

<description>Get mp3 ringtones, games, mobile videos,...</description>
<mc:resultType>M</mc:resultType>
<mc:displayUrl>downwap.com</mc:displayUrl>
</item>
-
<item>
<title>Wallpapers</title>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/5
</link>
<description>... and images - Pac Man - Top Ten - Hot...</description>

<mc:resultType>M</mc:resultType>
<mc:displayUrl>downl.co.uk</mc:displayUrl>
</item>
-
<item>
<title>Mp3-Ringtones.Mobi</title>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/6
</link>
<description>Get free downloads, ringtones, wallpapers,...</description>
<mc:resultType>M</mc:resultType>

<mc:displayUrl>www.mpones.mobi</mc:displayUrl>
</item>
-
<item>
<title>Cartoonscape</title>
-
<link>
http://www.moton.com/w/cp/7b517282-0369-4665-8df6-240d02204cc4/M/7
</link>
<description>Get free cartoons, movie clips, wallpapers,...</description>
<mc:resultType>M</mc:resultType>
<mc:displayUrl>scape.com</mc:displayUrl>

</item>
</channel>
</rss>
mobimad is offline  
Reply With Quote
Old 09-22-2010, 02:33 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

What have you written so far that's failing?
__________________
My Blog
Enfernikus is offline  
Reply With Quote
Old 09-22-2010, 03:18 PM   #3 (permalink)
The Visitor
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
mobimad is on a distinguished road
Default

Sorry this is the php i am using, but i cannot echo the <ad:advertisement> info on the page, please help.

Code:
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://domaintofile.com/search/file.xml');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);


$xml = new SimpleXMLElement($data);

foreach($xml->channel as $post) {

//Use namespace, this works
    $namespaces = $post->getNameSpaces(true);
   $openSearch = $post->children($namespaces['openSearch']); 
    echo "total results $openSearch->totalResults"; echo "<br/>";
    echo " Start Index $openSearch->startIndex";  echo "<br/>";
    echo "Items Per Page $openSearch->itemsPerPage";  echo "<br/><br/>";
	


}

//This bit will not show the title, subTitle or displayUrl for <ad:advertisement> 

foreach($xml->channel as $post) {

//Use namespace 
    $ad = $post->children("http://a9.com/-/opensearch/extensions/advertisement/1.0/");
     echo $ad->title;
    echo $ad->subTitle;
    echo $ad->displayUrl;
}


foreach($xml->channel->item as $post) {
   printf('<div class="title_link">Visit <a href="%s">%s</a><br /></div>' . "\n",  $post->link, $post->title);
	echo "<div class='description'>$post->description </div>";
	
	
	//Use the namespace
	$namespaces = $post->getNameSpaces(true);
    $mc = $post->children($namespaces['mc']); 
    echo $mc->displayUrl;
    echo "<br/><br/>";
 
	
}

?>
mobimad is offline  
Reply With Quote
Old 09-22-2010, 07:28 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Hi Lucy, welcome to TalkPHP.

Just a quicky question before trying to help out, are you wanting to use an XSL template (like in the other thread that you posted in) or to continue using SimpleXML like in your code above?
Salathe is offline  
Reply With Quote
Old 09-22-2010, 08:42 PM   #5 (permalink)
The Visitor
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
mobimad is on a distinguished road
Default

Hi salathe,

To be honest i dont mind which way, i've been trying everything for 3 days now and followed dozens of tutorials, i'm pretty lost, i really hope you can help me.

Please feel free to show me anyway that will work, i just want to be able to display the <ad:advertisement> data and the <item> data on a page.

Thankyou, Lucy
mobimad is offline  
Reply With Quote
Old 09-27-2010, 09:55 AM   #6 (permalink)
The Visitor
 
Join Date: Sep 2010
Posts: 4
Thanks: 0
mobimad is on a distinguished road
Default

Can you not help either?

Lucy
mobimad is offline  
Reply With Quote
Old 09-27-2010, 06:44 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Hi Lucy, sorry for missing this thread!

You were very nearly there with your code above, just a couple of things were a bit strange. Below is some code which is very similar to yours but doesn't output anything; it's just to show how to access the information that you're looking for.

Any questions about what it does differently, and why, just shout.

PHP Code:
// Search results statistics
// No need to loop here, there is only ever 
// one block of openSearch numbers
$opensearch $xml->channel->children('openSearch'TRUE);
$total      $opensearch->totalResults;
$start      $opensearch->startIndex;
$per_page   $opensearch->itemsPerPage;

// Loop over the <ad:advertisement> nodes
foreach ($xml->channel->children('ad'TRUE)->advertisement as $advert) {
    
$title     $advert->title;
    
$sub_title $advert->subTitle;
    
$disp_url  $advert->displayUrl;
}

// Loop over the <item> nodes, access <mc:displayUrl> nodes
foreach($xml->channel->item as $post) {
    
$title    $post->title;
    
$desc     $post->description;
    
$url      $post->link;
    
$disp_url $post->children('mc'TRUE)->displayUrl;

Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Injection and mysql_real_escape_string Durux General 61 01-29-2013 12:20 PM
Venerable methods and the applications they are commonly trusted in. Village Idiot Tips & Tricks 7 11-06-2008 07:36 AM
PHP Namespace Issue - What do you guys think? Wildhoney The Lounge 7 10-28-2008 02:34 PM
Menu Display Problem in IE6+ obolus XHTML, HTML, CSS 14 03-01-2008 07:57 PM
Tips: PHP security Village Idiot Tips & Tricks 22 11-23-2007 11:17 PM


All times are GMT. The time now is 07:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design