1

我有一个缓存远程 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 格式。有人可以帮忙调整这个脚本吗?这是给我的,我对它有点陌生。

4

2 回答 2

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)); 

生病留给你整合:-)

于 2010-05-24T19:08:24.677 回答
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 ) );

于 2010-05-24T19:05:42.083 回答