xslt 转换乱码

编程入门 行业动态 更新时间:2024-10-10 13:18:05

xslt 转换<a href=https://www.elefans.com/category/jswz/34/1768993.html style=乱码"/>

xslt 转换乱码

xslt 转换乱码

Delicious bookmarks with XSLT. Today we will create application which will show our Delicious bookmarks. We will use XSLT transformation to parse XML provided by Delicious. I will using my profile for our sample. And here are path for my XML feeds: . So, we have all for beginning. Today I`ll show you how to display this information in your projects.

带有XSLT的美味书签。 今天,我们将创建将显示我们的Delicious书签的应用程序。 我们将使用XSLT转换来解析Delicious提供的XML。 我将使用我的个人资料作为样本。 这是我的XML feed的路径:。 因此,我们已经开始。 今天,我将向您展示如何在您的项目中显示此信息。

Here are samples and downloadable package:

以下是示例和可下载的软件包:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the source files and lets start coding !

好的,下载源文件并开始编码!

步骤1. PHP (Step 1. PHP)

Ok, here are all used PHP files:

好的,这是所有使用过PHP文件:

index.php (index.php)

This file will generate list of Delicious bookmarks. Quite all code commented, so I hope it will easy to understand it.

该文件将生成Delicious书签列表。 相当多的代码都带有注释,因此希望它易于理解。


<?php
$sMethod = '';
$sXmlSrc = getDeliciousXml($sMethod);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($sXmlSrc);
// Load XSLT
$xsl = new DOMDocument;
$xsl->load('xslt/delicious.xslt');
$proc = new XSLTProcessor; // configure the transformer
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
// this function get delicious information using caches (not more than 1 times per hour)
function getDeliciousXml($sUrl) {// our folder with cache files$sCacheFolder = 'cache/';// cache filename$sFilename = date('YmdH').'.xml';if (! file_exists($sCacheFolder.$sFilename)) {$ch = curl_init($sUrl);$fp = fopen($sCacheFolder.$sFilename, 'w');curl_setopt($ch, CURLOPT_FILE, $fp);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_HTTPHEADER, Array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15') ); // makes our request look like it was made by Firefoxcurl_exec($ch);curl_close($ch);fclose($fp);}return file_get_contents($sCacheFolder.$sFilename);
}
?>

<?php
$sMethod = '';
$sXmlSrc = getDeliciousXml($sMethod);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($sXmlSrc);
// Load XSLT
$xsl = new DOMDocument;
$xsl->load('xslt/delicious.xslt');
$proc = new XSLTProcessor; // configure the transformer
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
// this function get delicious information using caches (not more than 1 times per hour)
function getDeliciousXml($sUrl) {// our folder with cache files$sCacheFolder = 'cache/';// cache filename$sFilename = date('YmdH').'.xml';if (! file_exists($sCacheFolder.$sFilename)) {$ch = curl_init($sUrl);$fp = fopen($sCacheFolder.$sFilename, 'w');curl_setopt($ch, CURLOPT_FILE, $fp);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_HTTPHEADER, Array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15') ); // makes our request look like it was made by Firefoxcurl_exec($ch);curl_close($ch);fclose($fp);}return file_get_contents($sCacheFolder.$sFilename);
}
?>

步骤2. CSS (Step 2. CSS)

Here are used CSS file. Just few styles for our demo:

这是用过CSS文件。 我们的演示仅提供几种样式:

css / styles.css (css/styles.css)

h1{text-align:center}
.dl-unit{background-color:#F3F3F3;border:1px solid #CCC;position:relative;margin:10px;padding:10px}
.dl-unit h3, .dl-unit div{border-bottom:1px dashed #888;padding:5px;overflow:hidden}
.dl-unit div p{float:left;margin-right:20px}
.dl-unit div p:first-child{font-weight:bold}
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.main{background:#FFF;width:900px;font-size:80%;border:1px #000 solid;margin:3.5em auto 2em;padding:1em 2em 2em}

h1{text-align:center}
.dl-unit{background-color:#F3F3F3;border:1px solid #CCC;position:relative;margin:10px;padding:10px}
.dl-unit h3, .dl-unit div{border-bottom:1px dashed #888;padding:5px;overflow:hidden}
.dl-unit div p{float:left;margin-right:20px}
.dl-unit div p:first-child{font-weight:bold}
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.main{background:#FFF;width:900px;font-size:80%;border:1px #000 solid;margin:3.5em auto 2em;padding:1em 2em 2em}

步骤3. XSLT (Step 3. XSLT)

And, most interesting part of my story – used XSLT rules:

而且,我故事中最有趣的部分–使用了XSLT规则:

xslt / delicious.xslt (xslt/delicious.xslt)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""><xsl:template match="channel"><xsl:variable name="channelDescription" select="description"/><xsl:variable name="authorLink" select="link"/><html><head><link media="all" href="css/styles.css" type="text/css" rel="stylesheet"/></head><body><div class="main"><h1><a><xsl:attribute name="href"><xsl:value-of select="$authorLink"/></xsl:attribute><xsl:value-of select="$channelDescription"/></a></h1><xsl:for-each select="item"><div class="dl-unit"><h3><a target="_blank"><xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute><xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute><xsl:value-of select="title"/></a></h3><div><p>Description:</p><p><xsl:value-of select="description" disable-output-escaping="yes" /></p></div><div><a target="_blank"><xsl:attribute name="href"><xsl:value-of select="comments"/></xsl:attribute>link to comments</a></div><div><p>When:</p><p><xsl:value-of select="substring(pubDate, 0, 26)"/></p></div> <!-- we will take first 26 symbols --><div><p>Tags:</p><p><xsl:for-each select="category"><a target="_blank"><xsl:attribute name="href"><xsl:value-of select="@domain"/><xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></a> |</xsl:for-each></p></div></div></xsl:for-each></div><xsl:comment>Copyright: AndrewP</xsl:comment></body></html></xsl:template>
</xsl:stylesheet>

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""><xsl:template match="channel"><xsl:variable name="channelDescription" select="description"/><xsl:variable name="authorLink" select="link"/><html><head><link media="all" href="css/styles.css" type="text/css" rel="stylesheet"/></head><body><div class="main"><h1><a><xsl:attribute name="href"><xsl:value-of select="$authorLink"/></xsl:attribute><xsl:value-of select="$channelDescription"/></a></h1><xsl:for-each select="item"><div class="dl-unit"><h3><a target="_blank"><xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute><xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute><xsl:value-of select="title"/></a></h3><div><p>Description:</p><p><xsl:value-of select="description" disable-output-escaping="yes" /></p></div><div><a target="_blank"><xsl:attribute name="href"><xsl:value-of select="comments"/></xsl:attribute>link to comments</a></div><div><p>When:</p><p><xsl:value-of select="substring(pubDate, 0, 26)"/></p></div> <!-- we will take first 26 symbols --><div><p>Tags:</p><p><xsl:for-each select="category"><a target="_blank"><xsl:attribute name="href"><xsl:value-of select="@domain"/><xsl:value-of select="."/></xsl:attribute><xsl:value-of select="."/></a> |</xsl:for-each></p></div></div></xsl:for-each></div><xsl:comment>Copyright: AndrewP</xsl:comment></body></html></xsl:template>
</xsl:stylesheet>

第4步。重要注意事项 (Step 4. Few important notes)

During checking my PHP file you will notice that I perform caching of Delicious requests. I don`t load provided RSS often that once per hour. No need overload Delicious server with often requests :)

在检查我PHP文件期间,您会注意到我执行了Delicious请求的缓存。 我每小时不加载一次RSS。 无需过载经常请求的美味服务器:)

I made ‘cache’ folder with permission to write (you should create ‘cache’ folder too).

我取得了“ cache”文件夹的写权限(您也应该创建“ cache”文件夹)。

Also I created .htaccess file in it with next content:

我也用下面的内容在其中创建了.htaccess文件:

Options -Indexes

选项-索引

That’s all.

就这样。

现场演示

结论 (Conclusion)

I hope that today’s article will very useful for your projects. Good luck!

我希望今天的文章对您的项目非常有用。 祝好运!

翻译自: /

xslt 转换乱码

更多推荐

xslt 转换乱码

本文发布于:2024-03-04 01:05:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:乱码   xslt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!