Skip to content

Instantly share code, notes, and snippets.

@hbsnow
Created March 27, 2013 05:46
Show Gist options
  • Save hbsnow/5251985 to your computer and use it in GitHub Desktop.
Save hbsnow/5251985 to your computer and use it in GitHub Desktop.
XHTML5 から HTML を出力する XSL。汎用的なものではありません
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="x rdf skos"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01//EN" doctype-system="http://www.w3.org/TR/html4/strict.dtd" indent="no"/>
<xsl:template match="x:head">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<meta http-equiv="Content-Script-Type" content="text/javascript"/>
<xsl:apply-templates/>
</head>
</xsl:template>
<!-- header -->
<xsl:template match="x:header">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">header</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<!-- section -->
<xsl:template match="x:section[x:h1]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv1-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:section[x:h2]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv2-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:section[x:h3]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv3-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:section[x:h4]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv4-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:section[x:h5]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv5-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:section[x:h6]">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">lv6-section</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:article">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">article</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="x:nav">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">nav</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<!-- footer -->
<xsl:template match="x:footer">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">footer</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<!-- figure -->
<xsl:template match="x:figure">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">figure</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<!-- figcaption -->
<xsl:template match="x:figcaption">
<div>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">figcaption</xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<!-- time -->
<xsl:template match="x:time">
<span>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">time</xsl:with-param>
</xsl:call-template>
</span>
</xsl:template>
<!-- mark -->
<xsl:template match="x:mark">
<span>
<xsl:call-template name="child">
<xsl:with-param name="attr-name">mark</xsl:with-param>
</xsl:call-template>
</span>
</xsl:template>
<!-- abbr -->
<xsl:key name="abbr-key" match="rdf:RDF/skos:Concept" use="skos:altLabel"/>
<xsl:template match="x:abbr">
<xsl:choose>
<xsl:when test="@title">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:when>
<xsl:when test="document('http://4uing.net/common/rdf/concept.rdf')/rdf:RDF/skos:Concept[skos:altLabel = current()]">
<xsl:apply-templates select="document('http://4uing.net/common/rdf/concept.rdf')/rdf:RDF/skos:Concept[skos:altLabel = current()]">
<xsl:with-param name="abbr" select="self::node()"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="rdf:RDF/skos:Concept">
<xsl:param name="abbr"/> <abbr title="{key('abbr-key', $abbr)/skos:prefLabel}"><xsl:value-of select="key('abbr-key', $abbr)/skos:altLabel"/></abbr>
</xsl:template>
<xsl:template match="x:datalist"></xsl:template>
<xsl:template name="child">
<xsl:param name="attr-name"/>
<xsl:attribute name="class">
<xsl:if test="@class">
<xsl:value-of select="@class"/>
<xsl:text></xsl:text>
</xsl:if>
<xsl:text>html5-</xsl:text>
<xsl:value-of select="$attr-name"/>
</xsl:attribute>
<xsl:apply-templates select="@*[not(name(self::node()) = 'class')]"/>
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*[name(self::node()) = 'role']"></xsl:template>
<xsl:template match="@*[name(self::node()) = 'itemtype']"></xsl:template>
<xsl:template match="@*[name(self::node()) = 'itemprop']"></xsl:template>
<xsl:template match="@*[name(self::node()) = 'itemscope']"></xsl:template>
<xsl:template match="@*[name(self::node()) = 'xml:base']"></xsl:template>
<xsl:template match="@*|text()|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="x rdf skos"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:x="http://www.w3.org/1999/xhtml"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:skos="http://www.w3.org/2004/02/skos/core#"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" doctype-system="about:legacy-compat" indent="no"/>
<xsl:key name="abbr-key" match="rdf:RDF/skos:Concept" use="skos:altLabel"/>
<xsl:template match="x:abbr">
<xsl:choose>
<xsl:when test="@title">
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:when>
<xsl:when test="document('http://4uing.net/common/rdf/concept.rdf')/rdf:RDF/skos:Concept[skos:altLabel = current()]">
<xsl:apply-templates select="document('http://4uing.net/common/rdf/concept.rdf')/rdf:RDF/skos:Concept[skos:altLabel = current()]">
<xsl:with-param name="abbr" select="self::node()"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="rdf:RDF/skos:Concept">
<xsl:param name="abbr"/>
<abbr title="{key('abbr-key', $abbr)/skos:prefLabel}"><xsl:value-of select="key('abbr-key', $abbr)/skos:altLabel"/></abbr>
</xsl:template>
<xsl:template match="node()|@*" priority="-1.0">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment