我有一个缓存远程XML文件的PHP脚本。我希望在缓存之前对XSL进行转换,但不知道如何执行此操作:
<?php
// Set this to your link Id
$linkId = "0oiy8Plr697u3puyJy9VTUWfPrCEvEgJR";
// Set this to a directory that has write permissions
// for this script
$cacheDir = "temp/";
$cachetime = 15 * 60; // 15 minutes
// Do not change anything below this line
// unless you are absolutely sure
$feedUrl="http://mydomain.com/messageService/guestlinkservlet?glId=";
$cachefile = $cacheDir .$linkId.".xml";
header('Content-type: text/xml');
// Send from the cache if $cachetime is not exceeded
if (file_exists($cachefile) && (time() - $cachetime
< filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->\n";
exit;
}
$contents = file_get_contents($feedUrl . $linkId);
// show the contents of the XML file
echo $contents;
// write it to the cache
$fp = fopen($cachefile, 'w');
fwrite($fp, $contents);
fclose($fp);
?>
这是我想用来转换它的XSL字符串:
<xsl:template match="/">
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<xsl:apply-templates select="messageList" />
</Document>
</kml>
</xsl:template>
<xsl:template match="messageList">
<name>My Generated KML</name>
<xsl:apply-templates select="message" />
</xsl:template>
<xsl:template match="message">
<Placemark>
<name><xsl:value-of select="esnName" /></name>
<Point>
<coordinates>
<xsl:value-of select="latitude" />,<xsl:value-of select="longitude" />
</coordinates>
</Point>
</Placemark>
</xsl:template>
我想实际转换XML输入并保存/返回KML格式。有人可以帮忙调整这个脚本吗?这是给了我的,我对它有点新意。
答案 0 :(得分:1)
$domOrigin = new DOMDocument('1.0');
$domOrigin->loadXML($contents);
$domXsl = new DOMDocument('1.0');
$domXsl->load('/path/to/stylesheet.xsl',LIBXML_NOCDATA);
$processor = new XSLTProcessor();
$processor->importStylesheet($domXsl);
file_put_contents($cachfile, $processor->transformToXml($domOrigin));
我会留给你整合: - )
答案 1 :(得分:0)
使用XSL扩展,这里有一个例子:
http://www.php.net/manual/en/book.xsl.php#90510
将您的file_get_contents调用替换为:
$XML = new DOMDocument();
$XML->load( $feedUrl . $linkId );
/*
this is the same as the example:
*/
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( $xslFile );
$xslt->importStylesheet( $XSL );
然后将“打印”行替换为file_put_contents( $cachefile, $xslt->transformToXML( $XML ) );