TOP XML & CGI 目次

XMLでデータベース
XML文書のデータ構造
XML文書の中身dtb.xml
<?xml version="1.0" encoding="Shift_JIS"?><?xml-stylesheet type="text/xsl" href="dtb.xsl"?>
<新聞雑誌記事><記事>あああ</記事>
<記事 タイトル="みっこサーバー" サブタイトル="サブテスト" 文献="手すとん" 人物="池田" 年月日="20040531" 連続="" 画像="" 横サイズ="" 縦サイズ="" リンク名="" リンク="" コメント="">本日は晴天なり</記事></新聞雑誌記事>
これをブラウザで見ると下のようになります。(スタイルシートを使って背景色や表組を設定しています)
スタイルシート読みとり部分<?xml-stylesheet type="text/xsl" href="dtb.xsl"?>をはずしてブラウザで見ると、データ構造が一目できます。
スタイルシートdtb.xslファイルは以下の通りです。CGIで編集するのはデータ本体が納められたXML文書(dtb.xml)のみでありデータの活用性に富んでいます。
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="kiji.xsl"/>

<xsl:template match="/">
<html>
<head><title>新聞雑誌記事分類</title></head>
<body bgcolor="#bbccbb" text="#000000" LINK="#FFFF80" VLINK="#80FF80" ALINK="#FF00FF">
<xsl:call-template name="目次"/>
<xsl:apply-templates select="新聞雑誌記事/*"/>
<xsl:call-template name="目次"/>
</body>
</html>
</xsl:template>


<xsl:template match="新聞雑誌記事/*">
<h2><text>記事</text></h2>
<xsl:call-template name="記事"/>
</xsl:template>

(中略)

<!--以下書式設定-->

<xsl:template name="記事">
<font size="5"><xsl:copy/></font>
<table width="760" align="center" border="1">
<xsl:if test="参照/@タイトル!='' or */参照/@タイトル!=''">
<font size="2">……参照させていただいたWeb……</font>
</xsl:if>
<xsl:apply-imports/>
</table>
<xsl:if test="*/記事/@タイトル!='' or 記事/@タイトル!=''">
<font size="3">    <xsl:copy/>終わり</font><br/>
</xsl:if>
<br/>
</xsl:template>


<xsl:include href="dbmenu.xsl"/>


</xsl:stylesheet>

上のスタイルシートは更にkiji.xslスタイルシートを呼び出しています。以下はkiji.xslファイルです。
実は更に、ページの上下に目次を表示するためにdbmenu.xslも呼び出していますが省略します。
<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="記事|参照|移動">
<xsl:apply-templates select="移動"/>
<xsl:apply-templates select="参照"/>
<xsl:apply-templates select="記事"/>
</xsl:template>


<xsl:template match="記事">
<xsl:for-each select=".">
<xsl:call-template name="記事個別"/>
</xsl:for-each>
</xsl:template>


<xsl:template match="参照">
<xsl:choose>
<xsl:when test="@タイトル!=''">
<font size="-1">
<xsl:for-each select=".">
<a>
<xsl:attribute name="href"><xsl:value-of select="@URL"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="@タイトル"/>
</a> 
</xsl:for-each>
</font>
</xsl:when>
</xsl:choose>
</xsl:template>


<xsl:template match="移動">
<xsl:choose>
<xsl:when test="@タイトル!=''">
<xsl:for-each select=".">
<a>
<xsl:attribute name="href"><xsl:value-of select="@URL"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<font color="blue"><xsl:value-of select="@タイトル"/></font>
</a> <xsl:value-of select="@年月日"/>
</xsl:for-each>
</xsl:when>
</xsl:choose>
</xsl:template>



<xsl:template name="記事個別">
<xsl:param name="back_color">#ffff66</xsl:param>
<xsl:choose>
<xsl:when test="@連続='' or @連続=3">
<xsl:for-each select=".">
<xsl:choose>
<xsl:when test="@連続=''">
<tr>
<td colspan="10" align="left" style="background:{$back_color}">
<b><xsl:value-of select="@タイトル"/></b></td>
</tr>
</xsl:when>
</xsl:choose>

<tr>
<xsl:choose>
<xsl:when test="@画像!=''">
<td rowspan="4" width="13%">
<font size="2"><xsl:value-of select="@文献"/></font><br/>
<xsl:value-of select="@人物"/><br/>
<xsl:value-of select="@年月日"/></td>
</xsl:when>
<xsl:otherwise>
<td rowspan="3" width="13%">
<font size="2"><xsl:value-of select="@文献"/></font><br/>
<xsl:value-of select="@人物"/><br/>
<xsl:value-of select="@年月日"/></td>
</xsl:otherwise>
</xsl:choose>

<td width="77%" align="center">
<b><xsl:value-of select="@サブタイトル"/></b></td>
<td width="10%" align="center">
<font color="navy">No.<xsl:number level="any"/></font></td>
</tr>
<xsl:call-template name="記事後半"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="記事連続"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template name="記事連続">
<xsl:param name="back_color">#ffff66</xsl:param>
<xsl:choose>
<xsl:when test="@連続=1">
<xsl:for-each select=".">
<tr>
<xsl:choose>
<xsl:when test="@画像!=''">
<td rowspan="4" width="13%"></td>
</xsl:when>
<xsl:otherwise>
<td rowspan="3" width="13%"></td>
</xsl:otherwise>
</xsl:choose>
<td width="77%" align="center">
<b><xsl:value-of select="@サブタイトル"/></b></td>
<td width="10%" align="center">
<font color="navy">No.<xsl:number level="any"/></font></td>
</tr>
<xsl:call-template name="記事後半"/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select=".">
<tr>
<xsl:choose>
<xsl:when test="@画像!=''">
<td rowspan="4" width="13%"></td>
</xsl:when>
<xsl:otherwise>
<td rowspan="3" width="13%"></td>
</xsl:otherwise>
</xsl:choose>
</tr>
<xsl:call-template name="記事後半"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>

</xsl:template>


<xsl:template name="記事後半">
<tr>
<td colspan="10"><xsl:value-of select="." disable-output-escaping="yes"/></td>
</tr>
<xsl:choose>
<xsl:when test="@画像!=''">
<tr>
<td width="88%" colspan="10">
<a>
<xsl:attribute name="href">
<xsl:value-of select="@画像"/>
</xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<img>
<xsl:attribute name="src">
<xsl:value-of select="@画像"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="@横サイズ=''">
<xsl:attribute name="width">300</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="width">
<xsl:value-of select="@横サイズ"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="@縦サイズ=''">
<xsl:attribute name="height">200</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="height">
<xsl:value-of select="@縦サイズ"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</img>
</a>
</td>
</tr>
<tr>
<td width="88%" colspan="10">
<xsl:call-template name="コメント"/>
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<tr>
<td width="88%" colspan="10">
<xsl:call-template name="コメント"/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="コメント">
<font color="red"><xsl:value-of select="@コメント"/></font>
<xsl:if test="@リンク!=''">
<xsl:if test="@コメント!=''"><br/></xsl:if>
<a>
<xsl:attribute name="href"><xsl:value-of select="@リンク"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<font color="blue"><xsl:value-of select="@リンク名"/></font>
</a>
</xsl:if>
</xsl:template>


</xsl:stylesheet>