version 3.1 checkin
[TestXSLT.git] / Distribution / More Examples / XML Prettyprinter / xml-prettyprinter.xslt
1 <?xml version='1.0' encoding='iso-8859-1'?>
2
3 <!-- xml pretty printing xslt -->
4
5 <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
6
7 <xsl:output method='html' version='1.0' encoding='iso-8859-1' indent='no'/>
8
9 <xsl:variable name="indent_text" select="'  '"/>
10
11 <xsl:template match="*[count(*) = 0]">
12 <xsl:param name="indent" select="0"/>
13 <xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template>
14 <xsl:element name="{name()}"><xsl:copy-of select="@*"/><xsl:value-of select="normalize-space(.)"/></xsl:element><xsl:text>
15 </xsl:text>
16 </xsl:template>
17
18 <xsl:template match="*[count(*) > 0]">
19 <xsl:param name="indent" select="0"/>
20 <xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template>
21 <xsl:element name="{name()}"><xsl:copy-of select="@*"/><xsl:text>
22 </xsl:text>
23 <xsl:apply-templates><xsl:with-param name="indent" select="$indent + 1"/></xsl:apply-templates>
24 <xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template></xsl:element><xsl:text>
25 </xsl:text>
26 </xsl:template>
27
28 <xsl:template name="indent">
29 <xsl:param name="count"/>
30
31 <xsl:if test="$count > 0">
32 <xsl:copy-of select="$indent_text"/>
33 <xsl:call-template name="indent">
34 <xsl:with-param name="count" select="$count - 1"/>
35 </xsl:call-template>
36 </xsl:if>
37
38
39 </xsl:template>
40
41
42
43 <xsl:template match="text()[string-length(normalize-space(.)) &lt; 1]">
44 </xsl:template>
45
46 <xsl:template match="text()[string-length(normalize-space(.)) > 1]">
47 <xsl:param name="indent" select="0"/>
48 <xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template>
49 <xsl:value-of select="normalize-space(.)"/><xsl:text>
50 </xsl:text>
51 </xsl:template>
52
53
54
55
56
57 </xsl:stylesheet>
58