看一下 PHP XSLT 处理器。
http://php.net/manual/en/class.xsltprocessor.php http://php.net/manual/en/class.xsltprocessor.php
使用方法如下:
<?php
$sXml = "<xml>";
$sXml .= "<sudhir>hello sudhir</sudhir>";
$sXml .= "</xml>";
# LOAD XML FILE
$XML = new DOMDocument();
$XML->loadXML( $sXml );
# START XSLT
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( 'xsl/index.xsl', LIBXML_NOCDATA);
$xslt->importStylesheet( $XSL );
#PRINT
print $xslt->transformToXML( $XML );
?>
(From http://php.net/manual/en/book.xsl.php http://php.net/manual/en/book.xsl.php)
UPDATE
您在评论中询问如何拦截来自特定用户代理(例如 Googlebot)的请求。有多种方法可以实现此目的,具体取决于您使用的 Web 服务器技术。
在 Apache 上,一种方法是使用模组重写 http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html在内部将请求的处理转移到包含与我们上面看到的代码类似的代码的 PHP 脚本。该脚本从最初请求的 URL 检索 XML 并将转换呈现给客户端。重写规则将有一个重写条件 http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteCond它将 HTTP_USER_AGENT 标头与 Google 的标头进行比较。这是规则的示例(未经测试,但您应该明白):
RewriteCond %{HTTP_USER_AGENT} ^(.*)Googlebot(.*)$ [NC]
RewriteRule ^(.*\.xml.*)$ /renderxslt.php?url=$1 [L]
简而言之,条件是寻找以字符串“googlebot”开头的引荐来源网址,重写规则匹配其中包含字符串“.xml”的任何 URL,并将完整 URL 作为查询字符串参数传递到 renderxslt.php 页面。
IIS 也存在 mod_rewrite 端口(http://www.isapirewrite.com/ http://www.isapirewrite.com/).
或者,通过 IIS,您可以使用 ASP.NETHTTP模块 http://www.c-sharpcorner.com/UploadFile/hemantkathuria/ASPNetHttpModules11262005004251AM/ASPNetHttpModules.aspx拦截请求,再次检查Request.Headers["HTTP_USER_AGENT"]
寻求谷歌的签名。然后,您可以通过读取 PHP 脚本生成的 HTML,或者使用 ASP.NET XML 控件以与上述类似的方式继续操作:
<asp:Xml ID="Xml1" runat="server" DocumentSource="~/cdlist.xml" TransformSource="~/listformat.xsl"></asp:Xml>