SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sitemap.php problem

    I am trying to create a sitemap using Chapter 5 of No nonsense XML Web Development. I have created a link to sitemap.php. Instead of opening in the browser it asks me if I want to open or save. Does anyone know what's wrong

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Are you sure your server is configured to serve files with the .php extension as
    application/x-httpd-php ??
    Do other *.php pages open OK?

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    index.php and doSearch.php work fine

    This is sitemap.php

    Code:
    <?php
    
    include_once 'common.inc.php';
    
    $timestamp = date('YmdHis');
    
    
    
    $xmlstring = '<?xml version="1.0"?>';
    
    $xmlstring .= '<sitemap created="' . $timestamp . '">';
    
    $handle = opendir($fileDir);
    
    while (($file = readdir($handle)) !== FALSE) {
    
    	if (is_dir($fileDir . $file)) continue;
    
    	if (!eregi("^(news|article|webcopy).*\.xml$", $file)) continue;
    
    
    
    	$xmlItem = simplexml_load_file($fileDir . $file);
    
    	if ((string)$xmlItem->status == 'live') {
    
    		$id = (string)$xmlItem['id'];
    
    		$type = ereg_replace('[0-9]', '', $id);
    
    		$created = ereg_replace('[^0-9]', '', $id);
    
    		$xmlstring .= '<content id="' . $id . '">';
    
    		$xmlstring .= '<headline>' . htmlspecialchars($xmlItem->headline) . '</headline>';
    
    		$xmlstring .= '<type>' . $type . '</type>';
    
    		$xmlstring .= '<created>' . $created . '</created>';
    
    		$xmlstring .= '</content>';
    
    	}
    
    }
    
    $xmlstring .= '</sitemap>';
    
    
    
    if (isset($_GET['sortby'])) {
    
    	$sortby = $_GET['sortby'];
    
    } else {
    
    	$sortby = 'headline';
    
    }
    
    
    
    $xml = simplexml_load_string($xmlstring);
    
    $xsl = new DOMDocument;
    
    $xsl->load('xslt/sitemap.xsl');
    
    $proc = new XSLTProcessor;
    
    $proc->importStyleSheet($xsl);
    
    $proc->setParameter('', 'SORTBY', $sortby);
    
    echo $proc->transformToXML($xml);
    
    ?>
    This is the xsl file

    Code:
    <xsl:stylesheet version="1.0"
    
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    
    	xmlns="http://www.w3.org/1999/xhtml">
    
    
    
    	<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"
    
    		media-type="application/xhtml+xml" encoding="iso-8859-1"
    
    		doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    		doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
    
    
    
    	<xsl:param name="SORTBY">headline</xsl:param>
    
    
    
    	<xsl:template match="/">
    
    		<html>
    
    			<head>
    
    				<title>Site Map</title>
    
    				<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1"/>
    
    			</head>
    
    			<body>
    
    				<h1>Site Map</h1>
    
    				<p>sort: <a href="?sortby=type">type</a> | <a href="?sortby=headline">headline</a> | <a href="?sortby=date">date</a></p>
    
    				<xsl:apply-templates/>
    
    			</body>
    
    		</html>
    
    	</xsl:template>
    
    
    
    	<xsl:template match="sitemap">
    
    		<ul>
    
          <xsl:choose>
    
            <xsl:when test="$SORTBY='type'">
    
              <xsl:apply-templates>
    
                <xsl:sort select="type" />
    
              </xsl:apply-templates>
    
            </xsl:when>
    
            <xsl:when test="$SORTBY='date'">
    
              <xsl:apply-templates>
    
                <xsl:sort select="created" />
    
              </xsl:apply-templates>
    
            </xsl:when>
    
            <xsl:otherwise>
    
              <xsl:apply-templates>
    
                <xsl:sort select="headline" />
    
              </xsl:apply-templates>
    
            </xsl:otherwise>
    
          </xsl:choose>
    
    		</ul>
    
    		<p><small>sorting by: <u><xsl:value-of select="$SORTBY"/></u></small></p>
    
    	</xsl:template>
    
    
    
    	<xsl:template match="content">
    
    		<li><a href="innerpage.php?id={@id}"><xsl:apply-templates select="headline"/></a> (<xsl:value-of select="type"/>)</li>
    
    	</xsl:template>
    
    	
    
    </xsl:stylesheet>
    Last edited by gleb; Sep 11, 2008 at 09:58. Reason: typo

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I'm have a bit of a handicap as I don't have the book (only the free sample chapters).

    If you "save" it, what is the name it saves as and what does the source look like?

    Anything like this?
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Site Map</title>
        <meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
      </head>
      <body>
        <h1>Site Map</h1>
        <p>sort: <a href="?sortby=type">type</a> | <a href="?sortby=headline">headline</a> | <a href="?sortby=date">date</a></p>
        <ul>
          <li><a href="innerpage.php?id=123">New XML application being built</a> ()</li>
          <li><a href="innerpage.php?id=123">XML-powered CMS Solutions</a> ()</li>
        </ul>
        <p>
          <small>sorting by: <u>headline</u></small>
        </p>
      </body>
    </html>
    Any error messages?

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It saves as sitemap.php and if opened it 's an empty file. If you've got chapter 4 then this sitemap is an addition to the CMS started there.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hmmm. I'll take a look at what I've got and think about it for a while and post back later when I get the chance.

    Edit:
    Well, the code as you posted seems to work for me OK.
    Note* I changed
    PHP Code:
    $xmlItem simplexml_load_file($fileDir $file); 
    to
    PHP Code:
    $xmlItem simplexml_load_file($fileDir '/' $file); 
    The only thing I can think of is to try and isolate your problem. Try these to see if they work
    xmlsitemap.php
    PHP Code:
    <?php
    include_once 'common.inc.php';
    $timestamp date('YmdHis');
    $xmlstring '<?xml version="1.0"?>';
    $xmlstring .= '<sitemap created="' $timestamp '">';
    $xmlstring .= '<content id="1">';
    $xmlstring .= '<headline>New XML application being built</headline>';
    $xmlstring .= '<type>XML</type>';
    $xmlstring .= '<created>2004-01-20</created>';
    $xmlstring .= '</content>';
    $xmlstring .= '<content id="2">';
    $xmlstring .= '<headline>XML-powered CMS Solutions</headline>';
    $xmlstring .= '<type>XML CMS</type>';
    $xmlstring .= '<created>2004-01-20</created>';
    $xmlstring .= '</content>';
    $xmlstring .= '</sitemap>';

    if (isset(
    $_GET['sortby'])) {
        
    $sortby $_GET['sortby'];
    } else {
        
    $sortby 'headline';
    }

    $xml simplexml_load_string($xmlstring);
    $xsl = new DOMDocument;
    $xsl->load('xslt/sitemap.xsl');
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl);
    $proc->setParameter('''SORTBY'$sortby);
    echo 
    $proc->transformToXML($xml);
    ?>
    xslsitemap.php
    PHP Code:
    <?php
    include_once 'common.inc.php';
    $timestamp date('YmdHis');
    $xmlstring '<?xml version="1.0"?>';
    $xmlstring .= '<sitemap created="' $timestamp '">';
    $handle opendir($fileDir);
    while ((
    $file readdir($handle)) !== FALSE) {
        if (
    is_dir($fileDir $file)) continue;
        if (!
    eregi("^(news|article|webcopy).*\.xml$"$file)) continue;
    //    $xmlItem = simplexml_load_file($fileDir . $file);
        
    $xmlItem simplexml_load_file($fileDir '/' $file);
        if ((string)
    $xmlItem->status == 'live') {
            
    $id = (string)$xmlItem['id'];
            
    $type ereg_replace('[0-9]'''$id);
            
    $created ereg_replace('[^0-9]'''$id);
            
    $xmlstring .= '<content id="' $id '">';
            
    $xmlstring .= '<headline>' htmlspecialchars($xmlItem->headline) . '</headline>';
            
    $xmlstring .= '<type>' $type '</type>';
            
    $xmlstring .= '<created>' $created '</created>';
            
    $xmlstring .= '</content>';
        }
    }
    $xmlstring .= '</sitemap>';

    if (isset(
    $_GET['sortby'])) {
        
    $sortby $_GET['sortby'];
    } else {
        
    $sortby 'headline';
    }

    $hard_xsl '
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns="http://www.w3.org/1999/xhtml">
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"
            media-type="application/xhtml+xml" encoding="iso-8859-1"
            doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
            doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
        <xsl:param name="SORTBY">headline</xsl:param>
        <xsl:template match="/">
            <html>
                <head>
                    <title>Site Map</title>
                    <meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1"/>
                </head>
                <body>
                    <h1>Site Map</h1>
                    <p>sort: <a href="?sortby=type">type</a> | <a href="?sortby=headline">headline</a> | <a href="?sortby=date">date</a></p>
                    <xsl:apply-templates/>
                </body>
            </html>
        </xsl:template>
        <xsl:template match="sitemap">
            <ul>
          <xsl:choose>
            <xsl:when test="$SORTBY=\'type\'">
              <xsl:apply-templates>
                <xsl:sort select="type" />
              </xsl:apply-templates>
            </xsl:when>
            <xsl:when test="$SORTBY=\'date\'">
              <xsl:apply-templates>
                <xsl:sort select="created" />
              </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates>
                <xsl:sort select="headline" />
              </xsl:apply-templates>
            </xsl:otherwise>
          </xsl:choose>
            </ul>
            <p><small>sorting by: <u><xsl:value-of select="$SORTBY"/></u></small></p>
        </xsl:template>
        <xsl:template match="content">
            <li><a href="innerpage.php?id={@id}"><xsl:apply-templates select="headline"/></a> (<xsl:value-of select="type"/>)</li>
        </xsl:template>
    </xsl:stylesheet>'
    ;

    $xml simplexml_load_string($xmlstring);
    $xsl_obj simplexml_load_string($hard_xsl);
    //$xsl = new DOMDocument;
    //$xsl->load('xslt/sitemap.xsl');
    $proc = new XSLTProcessor;
    //$proc->importStyleSheet($xsl);
    $proc->importStyleSheet($xsl_obj);
    $proc->setParameter('''SORTBY'$sortby);
    echo 
    $proc->transformToXML($xml);
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried just making the small change you suggested. No difference.
    Tried replacing my files with the ones you sent and I get the following errors

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /opt/lampp/htdocs/xml/05advxslt/cms/sitemap.php:2) in /opt/lampp/htdocs/xml/05advxslt/cms/common.inc.php on line 2

    Warning: DOMDocument::load() [function.DOMDocument-load]: Start tag expected, '<' not found in /opt/lampp/htdocs/xml/05advxslt/cms/xslt/sitemap.xsl, line: 8 in /opt/lampp/htdocs/xml/05advxslt/cms/sitemap.php on line 52

    Warning: XSLTProcessor::importStylesheet() [function.XSLTProcessor-importStylesheet]: compilation error in /opt/lampp/htdocs/xml/05advxslt/cms/sitemap.php on line 56

    Warning: XSLTProcessor::importStylesheet() [function.XSLTProcessor-importStylesheet]: xsltParseStylesheetProcess : empty stylesheet in /opt/lampp/htdocs/xml/05advxslt/cms/sitemap.php on line 56

    Warning: XSLTProcessor::transformToXml() [function.XSLTProcessor-transformToXml]: No stylesheet associated to this object in /opt/lampp/htdocs/xml/05advxslt/cms/sitemap.php on line 60

    Completely confused now. Why is there no reference to the stylesheet?

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    I'm not sure what you did. My version of common.inc.php looks like, but it may have been modified in a later chapter.
    PHP Code:
    <?php
    session_start
    ();
    $fileDir $_SERVER['DOCUMENT_ROOT'] . '/xml/';
    ?>
    Sorry if you misunderstood me. I meant for those 2 files to be tried separately to see if either worked. That is, does either xmlsitemap.php or xslsitemap.php work? If you didn't copy the code into their own files and test them, but tried incorporating the code into your sitemap.php file, I don't really know how you changed it, so I can't say what's causing those errors.

    Please put those 2 files in the same folder as the sitemap.php file and request them with your browser.

    And please specify which errors come from which file.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, they both worked. I find the second easiest to understand so I'll go with that

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,036
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Hmmm. Now I'm really wondering what's going on.

    In xmlsitemap.php I replaced the code that fetches the XML file content using reiterative directory searching with hard-coded XML to see if that was the problem area and kept the XML/XSL parsing part intact.

    In xslsitemap.php I changed the code that does the XML/XSL parsing to use hard-coded XSL and kept the XML fetching part intact.

    So if both work, then the original should work too. In other words, the first part gets XML and puts it into a string, and the second part gets the XSL and uses it to transform the XML string into HTML. So this should work too
    sitemap.php
    PHP Code:
    <?php
    include_once 'common.inc.php';
    $timestamp date('YmdHis');
    $xmlstring '<?xml version="1.0"?>';
    $xmlstring .= '<sitemap created="' $timestamp '">';
    $handle opendir($fileDir);
    while ((
    $file readdir($handle)) !== FALSE) {
        if (
    is_dir($fileDir $file)) continue;
        if (!
    eregi("^(news|article|webcopy).*\.xml$"$file)) continue;
    //    $xmlItem = simplexml_load_file($fileDir . $file);
        
    $xmlItem simplexml_load_file($fileDir '/' $file);
        if ((string)
    $xmlItem->status == 'live') {
            
    $id = (string)$xmlItem['id'];
            
    $type ereg_replace('[0-9]'''$id);
            
    $created ereg_replace('[^0-9]'''$id);
            
    $xmlstring .= '<content id="' $id '">';
            
    $xmlstring .= '<headline>' htmlspecialchars($xmlItem->headline) . '</headline>';
            
    $xmlstring .= '<type>' $type '</type>';
            
    $xmlstring .= '<created>' $created '</created>';
            
    $xmlstring .= '</content>';
        }
    }
    $xmlstring .= '</sitemap>';

    if (isset(
    $_GET['sortby'])) {
        
    $sortby $_GET['sortby'];
    } else {
        
    $sortby 'headline';
    }

    $xml simplexml_load_string($xmlstring);
    $xsl = new DOMDocument;
    $xsl->load('xslt/sitemap.xsl');
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl);
    $proc->setParameter('''SORTBY'$sortby);
    echo 
    $proc->transformToXML($xml);
    ?>
    Do you spot any differences with what you have/had? The only difference I can spot is
    PHP Code:
    //    $xmlItem = simplexml_load_file($fileDir . $file);
        
    $xmlItem simplexml_load_file($fileDir '/' $file); 
    Edit
    BTW, "headers already sent" errors occur when there is output before the call. This includes not so always obvious whitespace, and BOMs when the file is saved with the wrong content-type, usually an editor other than a plain text editor (i.e. Word instead of Notepad).

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what happened before but if I use the original sitemap.xsl it works!
    Gld it does coz I always thought it should


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
  •