Why can't I include PHP in XML file?

I have the following XML site map structure:

<?xml version="1.0" encoding="UTF-8"?>

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">



How can I include "<?php include ("assets/includes/menu.inc"); ?>" which have the following:

<li><a href="index.php">Home</a></li>
<li class="subMenu"><a href="gallery.php">Gallery</a>
        <li><a href="page1.php">Page</a></li>
        <li><a href="page2.php">Page2t</a></li>
<li><a href="contact.php">Contact</a></li>

What am I trying to do? I am trying to make my sitemap to be generated/updated automatically, so I want to include my menu "main links" to the XML and then use CSS "before & after selectors" to add the tags <url><loc> before every <a></a> and </url></loc> after, creating: <url><loc>http://www.example.com/</loc></url> which is what the sitemap is essentially built of.

Any ideas of how I can make this work?


The server reads .xml .html .css files as simple text with markup, in order to use php you need to have a .php ( with the default settings, you can modiffy the apache to read every file as php if you want - but that has no sense) and with taht being said, if you want anything which implies php, you need to make a php file that generates the file you need, not the other way around.

The fact that a file has .php extension it tells the server that he must parse that file as php, so the parsers starts and only runs data that is betwen ,while everything else remains untouched.

You have to include XML in your PHP and not the opposite.

Your PHP file must have and XML header content type :

header ("Content-Type:text/xml");

And then you display xml in your php file like this :

echo '<?xml version="1.0" encoding="UTF-8"?>

    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">



Keep in mind that you can't simply include html in your sitemap. You have to format your links with specific markup , which can be understood by bots.

Your site map url will be something like http://www.mysite.com/sitemap.php If the extension of sitemap is really important to you you can get an .xml with url rewriting. You can also specify the url of your sitemap in robots.txt.

Sitemap: http://www.example.com/sitemap.php

See more exemples on this site : http://www.sitemaps.org/protocol.html#submit_robots

I'm not really sure what your try to achieve with your CSS because sitemap are meant to be displayed, but usually xml is styled with a XSL

Need Your Help

How can I change a folder name in Visual Studio 2008 that contains a test project?


I created a project that has unit tests in Visual Studio 2008 using the Unit Test Wizard. It put that project inside a folder named "TestProject1". Now I want to rename "TestProject1" to a more

Progress view height in iOS 7

iphone ios objective-c ios7 uiprogressview

I want to increase the height of progress view in iOS 6 and below i am doing this using appearence method