Display data from simpleXML inside HTML tags

I want to display some data from a simpleXML file load. I am (just about) OK with loading the file and identifying the data that I want, but how can it be echoed inside the necessary HTML tags? Can PHP’s echo construct display content from an array inside plain text or does it require another method?

The data from the XML will be multiple sets of:

$article->date
$article->url
$article->title
$article->description

And the HTML is:

<li class="date">[date]</li>
<li><a href="">[title]</a></li>
<li class="desc">[description]</li>

I searched the forum but couldn’t find this exact problem.

Using XSLT:

/sp_articles.xml


<?xml version="1.0"?>
<articles>
	<article>
		<date>1/5/2009</date>
		<url>foo.com</url>
		<title>Articles Title</title>
		<description>Description</description>
	</article>
	<article>
		<date>1/5/2009</date>
		<url>foo.com</url>
		<title>Articles Title</title>
		<description>Description</description>
	</article>
	<article>
		<date>1/5/2009</date>
		<url>foo.com</url>
		<title>Articles Title</title>
		<description>Description</description>
	</article>
	<article>
		<date>1/5/2009</date>
		<url>foo.com</url>
		<title>Articles Title</title>
		<description>Description</description>
	</article>
	<article>
		<date>1/5/2009</date>
		<url>foo.com</url>
		<title>Articles Title</title>
		<description>Description</description>
	</article>
</articles>

/sp_articles_stylesheet.xsl


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions">

	<xsl:template match="/">
		<ul>
			<xsl:for-each select="/articles/child::*">
				<li>
					<ul>
						<li class="date"><xsl:value-of select="child::date"/></li>
						<li><a>
						  	<xsl:attribute name="href">
    							<xsl:value-of select="child::url"/>
  							</xsl:attribute>
							<xsl:value-of select="child::title"/>
							</a>
						</li>
						<li class="desc"><xsl:value-of select="child::description"/></li>
					</ul>
				</li>
			</xsl:for-each>
		</ul>
	</xsl:template>
	
</xsl:stylesheet>

/index.php


<?php
/*
* Load articles DOM document 
*/
$objArticles = new DOMDocument();
$objArticles->load('sp_articles.xml');
		
/*
* Load transform DOM document 
*/
$objStylesheet = new DOMDocument();
$objStylesheet->load('sp_articles_stylesheet.xsl');
		
/*
* Create XSLT processor ansd set stylesheet 
*/
$objXSLT = new XSLTProcessor(); 
$objXSLT->importStylesheet($objStylesheet); 
		
/*
* Apply transformation 
*/
$articlesHTML = $objXSLT->transformToXML($objArticles);	

/*
* Dump data
*/
echo $articlesHTML;
?>

Thanks for responding, but that looks like it might be something to learn in the future, rather than work with right now. I haven’t heard of XSLT. I’ve only just started trying out XML and PHP.

Could you provide the entire script and XML structure.

Its relatively straightforward process in theory:


$xml = simplexml_load_file('file.xml');
foreach($xml->children() as $article) {
    printf(
        '<li class="date">&#37;s</li>
         <li><a href="%s">%s</a></li>
         <li class="desc">%s</li>'
         ,(string) $article->date
         ,(string) $article->url
         ,(string) $article->title
         ,(string) $article->description
    );
}