Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created August 19, 2015 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/ecce393eb6056d4c92a7 to your computer and use it in GitHub Desktop.
Save komasaru/ecce393eb6056d4c92a7 to your computer and use it in GitHub Desktop.
XSL template to generate a HTML from a XML of MariaDB(MySQL)'s table definition.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" encoding="utf-8" version="1.0" />
<xsl:template match="mysqldump">
<html>
<head>
<title>データベース・テーブル定義</title>
<style type="text/css">
h2 {
background-color: #8fbc8f;
padding: 5px;
}
h4 {
color: #666;
margin-top: 5px;
margin-bottom: 5px;
}
table {
border-collapse: separate;
border-spacing: 0px;
border-top: 1px solid #ccc;
border-left: 1px solid #ccc;
margin-bottom: 5px;
}
table th {
text-align: left;
vertical-align: top;
color: #444;
background-color: #ccc;
border-top: 1px solid #fff;
border-left: 1px solid #fff;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
padding: 4px;
white-space: nowrap;
}
table td {
background-color: #fafafa;
border-right: 1px solid #ccc;
border-bottom: 1px solid #ccc;
padding: 4px;
white-space: nowrap;
}
.tbl-db-name {
margin-bottom: 10px;
}
.th-db-title {
background-color: #bdb76b;
width: 200px;
}
.th-tbl-title {
width: 200px;
}
.td-name {
width: 200px;
}
.th-no {
width: 50px;
text-align: right;
}
.th-field,
.th-type,
.th-extra,
.th-default,
.th-comment,
.th-keyname {
width: 200px;
}
.th-null,
.th-key {
width: 50px;
}
.th-columns {
width: 400px;
}
.th-nonunique {
width: 100px;
}
.td-no {
text-align: right;
}
</style>
</head>
<body>
<h2>データベース・テーブル定義書</h2>
<xsl:apply-templates select="database" />
</body>
</html>
</xsl:template>
<xsl:template match="database">
<table class="tbl-db-name">
<tr>
<th class="th-db-title">データベース名</th>
<td class="td-name"><xsl:value-of select="@name" /></td>
</tr>
</table>
<xsl:apply-templates select="table_structure" />
</xsl:template>
<xsl:template match="table_structure">
<table>
<tr>
<th class="th-tbl-title">テーブル名</th>
<td class="td-name"><xsl:value-of select="@name" /></td>
</tr>
</table>
<h4>カラム情報</h4>
<table>
<thead>
<tr>
<th class="th-no" >#</th>
<th class="th-field" >フィールド名</th>
<th class="th-type" >データ型</th>
<th class="th-null" >Null</th>
<th class="th-key" >キー</th>
<th class="th-extra" >Extra</th>
<th class="th-default">デフォルト</th>
<th class="th-comment">備考</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="field">
<tr>
<td class="td-no"><xsl:value-of select="position()"/></td>
<td><xsl:value-of select="@Field" /></td>
<td><xsl:value-of select="@Type" /></td>
<td><xsl:value-of select="@Null" /></td>
<td><xsl:value-of select="@Key" /></td>
<td><xsl:value-of select="@Extra" /></td>
<td><xsl:value-of select="@Default" /></td>
<td><xsl:value-of select="@Comment" /></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<h4>インデックス情報</h4>
<table>
<thead>
<tr>
<th class="th-keyname" >インデックス名</th>
<th class="th-columns" >カラムリスト</th>
<th class="th-nonunique">ユニーク</th>
<th class="th-comment" >備考</th>
</tr>
</thead>
<tbody>
<xsl:for-each select="key">
<xsl:variable name="key_name" select="@Key_name" />
<xsl:if test="not(preceding-sibling::key[@Key_name=$key_name])">
<tr>
<td><xsl:value-of select="$key_name" /></td>
<td>
<xsl:for-each select="../key[@Key_name=$key_name]">
<xsl:value-of select="@Column_name"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</td>
<td>
<xsl:choose>
<xsl:when test="@Non_unique='0'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</td>
<td><xsl:value-of select="@Comment" /></td>
</tr>
</xsl:if>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template name="no_increment">
<xsl:param name="no" />
<xsl:value-of select="$no + 1" />
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment