2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

XHTML5からXHTML Basic1.1への変換

Last updated at Posted at 2017-12-17

#XHTMLからフィーチャーフォン向けへの変換

今さらだが、フィーチャーフォン(ガラケー)用にWebサイトを変換するXSLTを投稿してみる。そのうち失われる内容なので記念碑みたいなものか。
文字中心の簡易なサイトならそれほど困らないはず。
Shift_JISの方がフィーチャーフォンには良いのだが、変換できない文字(UTF-8限定の文字)があるとエラーを起こすので下処理が必要になる。
かつては3キャリア別々にして絵文字も分けたりしていたけれど、今はこの程度のざっくり変換でも良いんじゃないかとか。


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:htm="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="htm">
<xsl:output method="xml"
encoding="UTF-8"
version="1.0"
indent="no"
doctype-public="-//W3C//DTD XHTML Basic 1.1//EN"
doctype-system="http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"/>

<xsl:template match="htm:head">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="htm:link[@rel='stylesheet']|htm:title"/>
</xsl:element>
</xsl:template>

<xsl:template match="htm:link[@rel='stylesheet']">
<link rel="stylesheet" type="text/css">
<xsl:attribute name="href"><xsl:value-of select="concat(substring-before(@href,'css/'),'css/mobile.css')"/></xsl:attribute>
</link>
</xsl:template>

<xsl:template match="htm:meta|htm:link|htm:script|htm:style|htm:header|htm:figure/htm:p|node()[self::text()='&#10;']"/>

<xsl:template match="htm:time[parent::htm:article]|htm:figcaption|htm:canvas">
<p><xsl:apply-templates select="@href|node()"/></p>
</xsl:template>

<xsl:template match="htm:div|htm:section|htm:aside|htm:nav|htm:article|htm:figure|htm:blockquote|htm:span|htm:abbr|htm:cite|htm:code|htm:dfn|htm:ins|htm:q|htm:samp|htm:time">
<xsl:apply-templates select="@href|node()"/>
</xsl:template>

<xsl:template match="htm:h1[htm:span]">
<h1><xsl:value-of select="text()"/><br/><xsl:value-of select="htm:span"/></h1>
</xsl:template>

<xsl:template match="htm:a[starts-with(@href,'#')]">
<xsl:apply-templates select="node()"/>
</xsl:template>

<xsl:template match="htm:table">
<table>
<xsl:apply-templates select="htm:caption"/>
<xsl:apply-templates select="htm:thead/*"/>
<xsl:apply-templates select="htm:tbody/*"/>
<xsl:apply-templates select="htm:tfoot/*"/>
</table>
</xsl:template>

<xsl:template match="htm:dt[htm:a]">
<xsl:copy>&#9632;<xsl:apply-templates select="@href|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="htm:dt">
<xsl:copy>&#9661;<xsl:apply-templates select="@href|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="htm:img">
<xsl:value-of select="@alt"/>
</xsl:template>

<xsl:template match="@href|node()">
<xsl:copy><xsl:apply-templates select="@href|node()"/></xsl:copy>
</xsl:template>

</xsl:stylesheet>
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?