cd807ed2e301c33d298c1ba36876e5a4e29689b0
[geeqie.git] / doc / wiki2docbook / headingsNormalizer / headingsNormalizer.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0"
3         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
4         xmlns:xhtml="http://www.w3.org/1999/xhtml"
5         xmlns="http://www.w3.org/1999/xhtml">
6   <xsl:output method="xml" indent="yes"/>
7
8   <!-- 
9     This stylesheet can be applied to xhtml documents. It ensures one one
10     h1 element exists per document. If the input document has only one h1 
11     element it just copies all nodes to the output, otherwise it adds a 
12     new top level (h1) heading and depromotes every existing heading to a
13     lower level (ie, h1s turn into h2s, h2s turn into h3s, etc).
14   -->
15
16   <xsl:param name="defaultHeading" select="'Chapter'"/>
17
18   <xsl:template match="xhtml:html" mode="addtoplevelheading">
19      <html>
20         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
21      </html>
22   </xsl:template>
23
24   <xsl:template match="xhtml:body" mode="addtoplevelheading">
25     <body><h1><xsl:value-of select="$defaultTopHeading"/></h1>
26         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
27     </body>
28   </xsl:template>
29
30   <xsl:template match="xhtml:h1" mode="addtoplevelheading">
31     <h2>
32         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
33     </h2>
34   </xsl:template>
35
36   <xsl:template match="xhtml:h2" mode="addtoplevelheading">
37     <h3>
38         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
39     </h3>
40   </xsl:template>
41
42   <xsl:template match="xhtml:h3" mode="addtoplevelheading">
43     <h4>
44         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
45     </h4>
46   </xsl:template>
47
48   <xsl:template match="xhtml:h4" mode="addtoplevelheading">
49     <h5>
50         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
51     </h5>
52   </xsl:template>
53
54   <xsl:template match="xhtml:h5" mode="addtoplevelheading">
55     <h6>
56         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
57     </h6>
58   </xsl:template>
59
60   <xsl:template match="xhtml:h6" mode="addtoplevelheading">
61     <section>
62         <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
63     </section>
64   </xsl:template>
65
66   <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1" mode="addtoplevelheading">
67     <xsl:copy>
68       <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
69     </xsl:copy>
70   </xsl:template>
71
72   <xsl:template match="img[@alt]" priority="-1">
73       <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" />
74   </xsl:template>
75
76   <xsl:template match="@*|node()|text()|comment()|processing-instruction()" priority="-1">
77     <xsl:choose>
78       <xsl:when test="count(//*[local-name()='h1'])=1">
79     <xsl:copy>
80       <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" />
81     </xsl:copy>
82       </xsl:when>
83       <xsl:otherwise>
84     <xsl:copy>
85       <xsl:apply-templates select="@*|node()|text()|comment()|processing-instruction()" mode="addtoplevelheading"/>
86     </xsl:copy>
87       </xsl:otherwise>
88     </xsl:choose>
89   </xsl:template>
90 </xsl:stylesheet>