Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XSLT/XPath 2.0

11 views
Skip to first unread message

tesi...@diary.ocn.ne.jp

unread,
Apr 2, 2008, 2:13:03 AM4/2/08
to
XSLT, XPath 2.0 の膨大な仕様書に途方に暮れていましたが、
この↓チュートリアルがなかなか良かったです:
http://zvon.org/xxl/XSL-Ref/Tutorials/index.html
このサイトのチュートリアルには、1.0 のときもお世話になったことがあります。

XSLT 覚えたてのご多分にもれず、LaTeXライクな XMLタグもつ文書を XHTML
に変換するスタイルシートを書いてみました。
このうち、索引を作成する部分を投稿します(XSLT/XPath 2.0 の機能を激しく
使った部分だから)。

LaTeX 索引作成の makeindex の \index{}:
\index{かん@環!きょくしょかん@局所環!きょくしょかんじょうのかぐん@局所環上の加群}
に対し、
<a:index r="きょくしょかんじょうのかぐん"
e1="局所環" r1="きょくしょかん" e2="環" e3="かん">局所環上の加群</a:index>
と書き(なぜか並べる順序が逆:-)。まぁ、いいや)、索引らしく整形したものを
<a:printindex/> がある位置に出力します。

XSLTスタイルシート(index.xsl) と、
java net.sf.saxon.Transform -s:sample.xml -xsl:index.xsl -o:sample.html
の sample.xml(入力XMLファイル), sample.html(変換結果)を添付します。
これを試してみよう、なんて酔狂な考えをお持ちでしたら、文字コードを UTF-8
に変更してください(nkf -w index.xsl とか、nkf -w sample.xml とか)。

使用した XSLT プロセッサ:
saxon ver. 9.0.0.1 (http://saxon.sourceforge.net/)
---------------------------- index.xsl ----------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:a="file:///home/tesigana/local/share/xml/article"
xmlns:w="file:///home/tesigana/local/share/xml/worknamespace"
exclude-result-prefixes="#default a w">
<xsl:output method="xml" indent="yes"
doctype-public="-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
doctype-system="http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd"/>

<xsl:template match="/">
<html>
<head><title>索引サンプル</title></head>
<body>
<h1>索引サンプル</h1>
<xsl:for-each select="//a:index">
<a id="{generate-id(.)}"><xsl:apply-templates/></a><br/>
</xsl:for-each>
<xsl:apply-templates select="//a:printindex"/>
</body>
</html>
</xsl:template>

<xsl:template match="a:printindex">
<h1>索引</h1>
<xsl:variable name="indexlist" as="node()*">
<xsl:apply-templates mode="normalize" select="//a:index"/>
</xsl:variable>
<xsl:if test="count($indexlist)">
<xsl:for-each-group select="$indexlist" group-by="w:r0">
<xsl:sort select="current-grouping-key()"/>
<h2 class="index"><xsl:value-of select="w:r0"/></h2>
<ul>
<xsl:call-template name="makeindex">
<xsl:with-param name="grp" select="current-group()"/>
<xsl:with-param name="dep" select="1"/>
</xsl:call-template>
</ul>
</xsl:for-each-group>
</xsl:if>
</xsl:template>

<xsl:template name="makeindex">
<xsl:param name="grp"/>
<xsl:param name="dep"/>
<xsl:for-each-group select="$grp"
group-by="if ($dep = 1) then w:r1 else if ($dep = 2) then w:r2 else w:r3">
<xsl:sort select="current-grouping-key()"/>
<li>
<xsl:apply-templates mode="makeindex"
select="if ($dep = 1) then w:e1 else if ($dep = 2) then w:e2 else w:e3"/>
<xsl:variable name="linklist" as="node()*">
<xsl:call-template name="enumlink">
<xsl:with-param name="grp" select="current-group()"/>
<xsl:with-param name="dep" select="$dep"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="count($linklist)">
<xsl:text> </xsl:text><xsl:copy-of select="$linklist"/>
</xsl:if>
<xsl:if test="count(current-group())>1">
<xsl:variable name="list" as="node()*">
<xsl:call-template name="makeindex">
<xsl:with-param name="grp" select="current-group()"/>
<xsl:with-param name="dep" select="$dep + 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="count($list)">
<ul>
<xsl:copy-of select="$list"/>
</ul>
</xsl:if>
</xsl:if>
</li>
</xsl:for-each-group>
</xsl:template>

<xsl:template name="enumlink">
<xsl:param name="grp"/>
<xsl:param name="dep"/>
<xsl:for-each select="$grp">
<xsl:choose>
<xsl:when test="$dep = 1"><xsl:if test="not(w:e2)"><a href="{w:id}">●</a>
</xsl:if></xsl:when>
<xsl:when test="$dep = 2"><xsl:if test="not(w:e3)"><a href="{w:id}">●</a>
</xsl:if></xsl:when>
<xsl:otherwise><a href="{w:id}">●</a>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

<xsl:template match="a:index" mode="normalize">
<w:e>
<w:id>#<xsl:value-of select="generate-id(.)"/></w:id>
<xsl:choose>
<xsl:when test="@e3">
<xsl:variable name="r1" select="if (@r3) then @r3 else @e3"/>
<xsl:variable name="r2" select="if (@r2) then @r2 else @e2"/>
<xsl:variable name="r3" select="if (@r) then @r else @e"/>
<xsl:variable name="i" select="upper-case(substring($r1, 1, 1))"/>
<w:r0><xsl:value-of select="$i"/></w:r0>
<w:e0><xsl:value-of select="$i"/></w:e0>
<w:r1><xsl:value-of select="$r1"/></w:r1>
<w:e1><xsl:value-of select="@e3"/></w:e1>
<w:r2><xsl:value-of select="$r2"/></w:r2>
<w:e2><xsl:value-of select="@e2"/></w:e2>
<w:r3><xsl:value-of select="$r3"/></w:r3>
<w:e3><xsl:apply-templates/></w:e3>
</xsl:when>
<xsl:when test="@e2">
<xsl:variable name="r1" select="if (@r2) then @r2 else @e2"/>
<xsl:variable name="r2" select="if (@r) then @r else @e"/>
<xsl:variable name="i" select="upper-case(substring($r1, 1, 1))"/>
<w:r0><xsl:value-of select="$i"/></w:r0>
<w:e0><xsl:value-of select="$i"/></w:e0>
<w:r1><xsl:value-of select="$r1"/></w:r1>
<w:e1><xsl:value-of select="@e2"/></w:e1>
<w:r2><xsl:value-of select="$r2"/></w:r2>
<w:e2><xsl:apply-templates/></w:e2>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="r1" select="if (@r) then @r else ."/>
<xsl:variable name="i" select="upper-case(substring($r1, 1, 1))"/>
<w:r0><xsl:value-of select="$i"/></w:r0>
<w:e0><xsl:value-of select="$i"/></w:e0>
<w:r1><xsl:value-of select="$r1"/></w:r1>
<w:e1><xsl:apply-templates/></w:e1>
</xsl:otherwise>
</xsl:choose>
</w:e>
</xsl:template>

</xsl:stylesheet>
---------------------------- sample.xml ----------------------------
<?xml version="1.0" encoding="utf-8"?>
<a:article xmlns:a="file:///home/tesigana/local/share/xml/article">

<a:index r="かんじゅんどうけい" e2="環" r2="かん">環凖同型</a:index>
<a:index r="あまりのいちいせい" e2="余り" r2="あまり">余りの一意性</a:index>
<a:index r="けいしきてきべききゅうすうかん" e2="環"
r2="かん">形式的冪級数環</a:index>
<a:index r="かいせきかんすうのめ">解析関数の芽</a:index>
<a:index r="きょくしょかん" e2="環" r2="かん">局所環</a:index>
<a:index r="きょくしょかんじょうのかぐん"
e2="局所環" r2="きょくしょかん" e3="環" r3="かん">局所環上の加群</a:index>
<a:index r="きょくしょかんじょうのかぐんのひょじゅんきてい"
e2="局所環" r2="きょくしょかん"
e3="環" r3="かん">局所環上の加群の標準基底</a:index>
<a:index r="あまり">余り</a:index>
<a:index r="top down じゅんじょ">top down 順序</a:index>
<a:index r="QR あるごりずむ"
e2="アルゴリズム" r2="あるごりずむ">QR アルゴリズム</a:index>
<a:index r="かぐんにたいするモラのあるごりずむ"
e2="アルゴリズム" r2="あるごりずむ">加群に対するモラのアルゴリズム</a:index>
<a:index r="きょくしょわりざんあるごりずむ"
e2="アルゴリズム" r2="あるごりずむ">局所割算アルゴリズム</a:index>
<a:index r="top down じゅんじょ">top down 順序</a:index>
<a:index r="あまり">余り</a:index>

<a:printindex/>
</a:article>
---------------------------- sampel.html ----------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>索引サンプル</title>
</head>
<body>
<h1>索引サンプル</h1>
<a id="d1e3">環凖同型</a>
<br/>
<a id="d1e6">余りの一意性</a>
<br/>
<a id="d1e9">形式的冪級数環</a>
<br/>
<a id="d1e12">解析関数の芽</a>
<br/>
<a id="d1e15">局所環</a>
<br/>
<a id="d1e19">局所環上の加群</a>
<br/>
<a id="d1e22">局所環上の加群の標準基底</a>
<br/>
<a id="d1e25">余り</a>
<br/>
<a id="d1e28">top down 順序</a>
<br/>
<a id="d1e31">QR アルゴリズム</a>
<br/>
<a id="d1e34">加群に対するモラのアルゴリズム</a>
<br/>
<a id="d1e38">局所割算アルゴリズム</a>
<br/>
<a id="d1e41">top down 順序</a>
<br/>
<a id="d1e44">余り</a>
<br/>
<h1>索引</h1>
<h2 class="index">T</h2>
<ul>
<li>top down 順序 <a href="#d1e28">●</a>
<a href="#d1e41">●</a>
</li>
</ul>
<h2 class="index">あ</h2>
<ul>
<li>余り <a href="#d1e25">●</a>
<a href="#d1e44">●</a>
<ul>
<li>余りの一意性 <a href="#d1e6">●</a>
</li>
</ul>
</li>
<li>アルゴリズム<ul>
<li>QR アルゴリズム <a href="#d1e31">●</a>
</li>
<li>加群に対するモラのアルゴリズム <a href="#d1e34">●</a>
</li>
<li>局所割算アルゴリズム <a href="#d1e38">●</a>
</li>
</ul>
</li>
</ul>
<h2 class="index">か</h2>
<ul>
<li>解析関数の芽 <a href="#d1e12">●</a>
</li>
<li>環<ul>
<li>環凖同型 <a href="#d1e3">●</a>
</li>
<li>局所環 <a href="#d1e15">●</a>
<ul>
<li>局所環上の加群 <a href="#d1e19">●</a>
</li>
<li>局所環上の加群の標準基底 <a href="#d1e22">●</a>
</li>
</ul>
</li>
<li>形式的冪級数環 <a href="#d1e9">●</a>
</li>
</ul>
</li>
</ul>
</body>
</html>

0 new messages