レ点腫瘍学ノート

pukiwikiカスタマイズ箇所/2021/sitemapとRSSにスタイルシート(XSL)を適用する の履歴ソース(No.9)

#author("2022-12-01T00:43:12+09:00;2021-06-18T18:25:39+09:00","default:tgoto","tgoto")
pukiwikiのサイトマッププラグイン(sitemap.inc.php)とRSSプラグイン(rss.inc.php)はいずれもそのままGoogle Search Consoleに登録できて非常に便利ですが見た目が素っ気ないので''拡張スタイルシート''(XSL: Extensible Stylesheet Language)を適用します。

ちなみに適用後の見た目はこのようになっています。
→ [[RSS>https://oncologynote.com/?cmd=rss]] [[sitemap>https://oncologynote.com/?cmd=sitemap]]

#contentsx(depth=1)

*sitemap.inc.phpの変更点 [#td66247f]

**119行目付近 [#se792e69]

下記の1行をXML宣言の直後の行に付加しておきます。
 < <?xml-stylesheet type="text/xsl" href="./skin/sitemap_css.xsl" ?>

**69行目付近 [#id6f4896]
短縮URLに対応させるための変更も加えています。
 <  $r_page = get_short_url_from_pagename($page);
 <  $link = $script . $r_page;
 ---
 >  $r_page = rawurlencode($page);
 >  $link = $script . '?' . $r_page;

*sitemap_css.xslの記載内容 [#g1197eab]

 <?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.com">oncologynote.com</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の変更点 [#ge7ad094]


**85行目付近 [#w1fbce6c]

スタイルシートを適用します。ここでは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行目付近 [#e8817e63]
ついでに日付も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の記載内容 [#sfa34342]

 <?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>

*ダウンロード [#xf613636]

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

&ref(sitemap.inc.php);
&ref(sitemap_css.xsl);
&ref(rss.inc.php);
&ref(rss_css.xsl);

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