レ点腫瘍学ノート

Top / pukiwikiカスタマイズ箇所 / 2021

sitemapとRSSにスタイルシート(XSL)を適用する

pukiwikiのサイトマッププラグイン(sitemap.inc.php)とRSSプラグイン(rss.inc.php)はいずれもそのままGoogle Search Consoleに登録できて非常に便利ですが見た目が素っ気ないので拡張スタイルシート(XSL: Extensible Stylesheet Language)を適用します。

ちなみに適用後の見た目はこのようになっています。
RSS sitemap

sitemap.inc.phpの変更点

119行目付近

下記の1行をXML宣言の直後の行に付加しておきます。

< <?xml-stylesheet type="text/xsl" href="./skin/sitemap_css.xsl" ?>

69行目付近

短縮URLに対応させるための変更も加えています。

<  $r_page = get_short_url_from_pagename($page);
<  $link = $script . $r_page;
---
>  $r_page = rawurlencode($page);
>  $link = $script . '?' . $r_page;

sitemap_css.xslの記載内容

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:html="http://www.w3.org/TR/REC-html40"
                xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>XML Sitemap</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
     body {
      font-family:'メイリオ', Meiryo, sans-serif;
      font-feature-settings: "pwid";
      font-size:14pt;
     }
     
     #intro {
      background-color:#CFEBF7;
      border:1px #2580B2 solid;
      padding:5px 13px 5px 13px;
      margin:10px;
     }
     
     #intro p {
      line-height: 16.8667px;
     }
     
     td {
      font-size:11px;
     }
     
     th {
      text-align:left;
      padding-right:30px;
      font-size:11px;
     }
     
     tr.high {
      background-color:whitesmoke;
     }
     
     #footer {
      padding:2px;
      margin:10px;
      font-size:8pt;
      color:gray;
     }
     
     #footer a {
      color:gray;
     }
     
     a {
      color:black;
     }
    </style>
   </head>
   <body>
    <h1>XML Sitemap</h1>
    <div id="intro">
     <p>
      This page is a XML Sitemap of <a href="https://oncologynote.jp">oncologynote.jp</a>.
     </p>
    </div>
    <div id="content">
     <table cellpadding="5">
      <tr style="border-bottom:1px black solid;">
       <th>URL</th>
       <th>Priority</th>
       <th>Change Frequency</th>
       <th>LastChange (GMT)</th>
      </tr>
      <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
      <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
      <xsl:for-each select="sitemap:urlset/sitemap:url">
       <tr>
        <xsl:if test="position() mod 2 != 1">
         <xsl:attribute  name="class">high</xsl:attribute>
        </xsl:if>
        <td>
         <xsl:variable name="itemURL">
          <xsl:value-of select="sitemap:loc"/>
         </xsl:variable>
         <a href="{$itemURL}">
          <xsl:value-of select="sitemap:loc"/>
         </a>
        </td>
        <td>
         <xsl:value-of select="concat(sitemap:priority*100,'%')"/>
        </td>        <td>
         <xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
        </td>
        <td>
         <xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
        </td>
       </tr>
      </xsl:for-each>
     </table>
    </div>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

rss.inc.phpの変更点

85行目付近

スタイルシートを適用します。ここではXSLスタイルシートのファイル名は./skin/rss_css.xslとしています。

< print '<?xml version="1.0" encoding="UTF-8"?>' . "\n\n";
---
> print '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<?xml-stylesheet type="text/xsl" href="./skin/rss_css.xsl" ?>
> ' . "\n\n";

45行目付近

ついでに日付もpukiwikiの他のページに揃えておきます。
もともとのプラグインの表記法だと「Fri, 18 Jun 2021 11:00:00 JST」となってしまっていたところを「2021-06-18 11:00:00」に書き換えることになります。

< $date = get_date('D, d M Y H:i:s T', $time);
---
> $date = get_date('Y-m-d H:i:s', $time);

rss_css.xslの記載内容

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:html="http://www.w3.org/TR/REC-html40"
                xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="channel">
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>XML RSS</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
     body {
      font-family:'メイリオ', Meiryo, sans-serif;
      font-feature-settings: "pwid";
      font-size:14pt;
     }
     h1{
      color:#000;
      font-size:1.6rem;
      margin:0.25em 10px;
      padding:0 2px 0.25em 0;
      border-bottom: 2px solid #e4e4e4;
      line-height:1.3em;
      word-break: break-all;
     }
     td {
      font-size:11px;
     }
     
     th {
      text-align:left;
      padding-right:30px;
      font-size:11px;
     }
     
     tr.high {
      background-color:whitesmoke;
     }
     
     #footer {
      padding:2px;
      margin:10px;
      font-size:8pt;
      color:gray;
     }
     
     #footer a {
      color:gray;
     }
     
     a {
      color:black;
     }
    </style>
   </head>
   <body>
    <h1>XML RSS</h1>
    <div id="content">
     <table cellpadding="5">
      <tr style="border-bottom:1px black solid;">
       <th>URL</th>
       <th>Title</th>
       <th>LastChange (GMT)</th>
      </tr>
      <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
      <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
      <xsl:for-each select="item">
       <tr>
        <xsl:if test="position() mod 2 != 1">
         <xsl:attribute  name="class">high</xsl:attribute>
        </xsl:if>
        <td>
         <xsl:variable name="itemURL">
          <xsl:value-of select="link"/>
         </xsl:variable>
         <a href="{$itemURL}">
          <xsl:value-of select="link"/>
         </a>
        </td>
        <td>
         <xsl:value-of select="title"/>
        </td>
        <td>
         <xsl:value-of select="pubDate"/>
        </td>
       </tr>
      </xsl:for-each>
     </table>
    </div>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

ダウンロード

いずれもpukiwiki 1.5.3をベースとした2021.6.18時点のファイルで、その後アップデートされていることがありますのでご注意ください。

filesitemap.inc.php
filesitemap_css.xsl
filerss.inc.php
filerss_css.xsl

この記事に対するコメント

このページには、まだコメントはありません。

お名前:


添付ファイル: filesitemap_css.xsl 393件 [詳細] filerss.inc.php 216件 [詳細] filerss_css.xsl 387件 [詳細] filesitemap.inc.php 319件 [詳細]

更新日:2021-06-18 閲覧数:572 views.