我可以将PHP包含在XML文件中吗?
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>
现在,我想替换< url>< loc> http://www.example.com/</loc></url>
与<?php include(assets / includes / menu.inc );?>
,其中包括以下内容:
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>
然而,我刚刚意识到PHP include不能用于XML文件,所以无论如何包括那个PHP?
However, I just realized that PHP include won't work on XML files, so is there anyway to include that php on there?
我在想什么?我试图让我的站点地图自动生成/更新,所以我希望将我的菜单主要链接包含到XML中,然后在选择器之前和之后使用CSS在每个之前添加标签< url>< loc>
< a>< / a>
和< / url>< / loc>
< url>< loc> http://www.example.com/< / loc>< / url>
。
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?
您的问题的答案是:是的,您可以使用XML嵌入PHP。 PHP专门设计为嵌入式:这就是为什么首先存在<?php ...?>
标签的原因。只要以下内容,PHP不知道或不关心你嵌入它的位置:
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:
- 该文件发送给PHP解释器。 >
- 主机文档中没有任何东西看起来像一个PHP开始标记。
- The file is sent to the PHP interpreter.
- There's nothing in the host document that inadvertently looks like a PHP start tag.
无关你给你想做什么。 Google站点地图是具有非常特定语法的XML文件。注入主菜单的HTML代码不会使网站地图变得不那么简单,就像在文本编辑器中打开MP3文件,并在中间输入您的名字不会让Freddy Mercury唱歌。
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.