SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    $article->date
    $article->url
    $article->title
    $article->description
    And the HTML is:
    Code:
    <li class="date">[date]</li>
    <li><a href="[url]">[title]</a></li>
    <li class="desc">[description]</li>
    I searched the forum but couldn't find this exact problem.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Using XSLT:

    /sp_articles.xml
    Code 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
    Code XML:
    <?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 Code:
    <?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;
    ?>

  3. #3
    SitePoint Addict
    Join Date
    Mar 2010
    Location
    UK
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Could you provide the entire script and XML structure.

    Its relatively straightforward process in theory:

    PHP Code:
    $xml simplexml_load_file('file.xml');
    foreach(
    $xml->children() as $article) {
        
    printf(
            
    '<li class="date">%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
        
    );



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •