-
Notifications
You must be signed in to change notification settings - Fork 0
/
reindent.xsl
84 lines (69 loc) · 2.79 KB
/
reindent.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet []>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--
reindent.xsl: a poor-man's Tidy
(produces nicely indented XML from poorly indented XML)
Author: Mike J. Brown <mike at skew.org>
Version: 2006-10-25
License: None; use freely.
This variation of the identity transform reindents the source document
by relying on the XSLT processor to remove whitespace-only text nodes,
then the stylesheet adds its own back in. The result is serialized
as XML with output indenting turned off.
-->
<!--Output without indenting because we're doing it ourselves -->
<xsl:output method="xml" omit-xml-declaration="yes"
version="1.0" indent="no" encoding="ascii" />
<!--Accept an external parameter specifying whether to delete comments -->
<xsl:param name="delete_comments" select="false()" />
<!--Accept an external parameter specifying the indent string (2 spaces
is the default) -->
<xsl:param name="indent_string" select="' '" />
<!--Remove whitespace-only text nodes from all elements -->
<xsl:strip-space elements="*" />
<!--Preserve whitespace-only text nodes in xsl:text elements -->
<xsl:preserve-space elements="xsl:text" />
<!-- Identity transform, but omit comments if $delete_comments was set,
and add whitespace where appropriate. -->
<xsl:template name="whitespace-before">
<xsl:if test="ancestor::*">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:for-each select="ancestor::*">
<xsl:value-of select="$indent_string" />
</xsl:for-each>
</xsl:template>
<xsl:template name="whitespace-after">
<xsl:if test="not(following-sibling::node())">
<xsl:text> </xsl:text>
<xsl:for-each select="../ancestor::*">
<xsl:value-of select="$indent_string" />
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="whitespace-before" />
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
<xsl:call-template name="whitespace-after" />
</xsl:template>
<xsl:template match="@*">
<xsl:copy />
</xsl:template>
<xsl:template match="processing-instruction()">
<xsl:call-template name="whitespace-before" />
<xsl:copy />
<xsl:call-template name="whitespace-after" />
</xsl:template>
<xsl:template match="comment()">
<xsl:if test="not($delete_comments)">
<xsl:call-template name="whitespace-before" />
<xsl:copy />
<xsl:call-template name="whitespace-after" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>