Module: PrettyXML

Defined in:
lib/pretty-xml.rb

Class Method Summary collapse

Class Method Details

.write(buffer) ⇒ Object



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
# File 'lib/pretty-xml.rb', line 9

def write(buffer)

xsl ="<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n<xsl:output method=\"xml\" encoding=\"UTF-8\"/>\n<xsl:param name=\"indent-increment\" select=\"'   '\"/>\n\n<xsl:template name=\"newline\">\n  <xsl:text disable-output-escaping=\"yes\">\n</xsl:text>\n</xsl:template>\n\n<xsl:template match=\"comment() | processing-instruction()\">\n  <xsl:param name=\"indent\" select=\"''\"/>\n  <xsl:call-template name=\"newline\"/>    \n  <xsl:value-of select=\"$indent\"/>\n  <xsl:copy />\n</xsl:template>\n\n<xsl:template match=\"text()\">\n  <xsl:param name=\"indent\" select=\"''\"/>\n  <xsl:call-template name=\"newline\"/>    \n  <xsl:value-of select=\"$indent\"/>\n  <xsl:value-of select=\"normalize-space(.)\"/>\n</xsl:template>\n  \n<xsl:template match=\"text()[normalize-space(.)='']\"/>\n\n<xsl:template match=\"*\">\n  <xsl:param name=\"indent\" select=\"''\"/>\n  <xsl:call-template name=\"newline\"/>    \n  <xsl:value-of select=\"$indent\"/>\n    <xsl:choose>\n     <xsl:when test=\"count(child::*) > 0\">\n      <xsl:copy>\n       <xsl:copy-of select=\"@*\"/>\n       <xsl:apply-templates select=\"*|text()\">\n         <xsl:with-param name=\"indent\" select=\"concat ($indent, $indent-increment)\"/>\n       </xsl:apply-templates>\n       <xsl:call-template name=\"newline\"/>\n       <xsl:value-of select=\"$indent\"/>\n      </xsl:copy>\n     </xsl:when>       \n     <xsl:otherwise>\n      <xsl:copy-of select=\".\"/>\n     </xsl:otherwise>\n   </xsl:choose>\n</xsl:template>    \n</xsl:stylesheet>\n"

  doc = Nokogiri::XML(buffer)
  xslt = Nokogiri::XSLT(xsl)
  out =  xslt.transform(doc)
  out.to_xml
end