I am trying to create an automatic "sitemap.xml", I already followed the instructions provided by Google to create one, and here is what I currently have:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://www.example.com/</loc></url>
</urlset>
Now, I wanted to replace "<url><loc>http://www.example.com/</loc></url>
" with "<?php include ("assets/includes/menu.inc"); ?>
" which will include the following:
<li><a href="index.php">Home</a></li>
<li class="subMenu"><a href="gallery.php">Gallery</a>
<ul>
<li><a href="404.php">404 Error Page</a></li>
<li><a href="about.php">About</a></li>
</ul>
</li>
<li><a href="contact.php">Contact</a></li>
However, I just realized that PHP include won't work on XML files, so is there anyway to include that php on there?
What am I thinking of? 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?
I, personally have never used include when creating a site map but I wouldn't see it as a problem as long as you are using valid xml elements.
You can create your xml using some of the php classes for it and just defining your content type like:
header ("Content-type: text/xml; charset=UTF-8");
and from there you can instantiate your xml classes if you chose or do it all by hand
The answer to your question is: yes, you can embedded PHP in XML. PHP is specifically designed to be embedded: that's why <?php...?>
tags exist in the first place. And PHP doesn't know or care where you embed it as long as:
- The file is sent to the PHP interpreter.
- There's nothing in the host document that inadvertently looks like a PHP start tag.
Such answer is basically irrelevant given what you want to do. Google sitemaps are XML files with a very specific syntax. Injecting the HTML code of your main menu will not make a sitemap out of it, just like opening a MP3 file in your text editor and typing your name in the middle of it will not make Freddy Mercury sing it.
来源:https://stackoverflow.com/questions/21446337/can-i-include-php-in-an-xml-file