Created
February 9, 2009 21:21
-
-
Save liyanage/61008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use IO::File; | |
my $xsltproc = "/usr/bin/xsltproc"; | |
my $data = do {undef local($/); <>}; | |
my $xslt = do {undef local($/); <DATA>}; | |
my $tempfile = "/tmp/xml-prettyprint.$$"; | |
my $xsltfile = "/tmp/xml-prettyprint.xslt"; | |
my $file = IO::File->new(">$tempfile") or die "unable to write-open temp file '$tempfile'"; | |
print $file $data; | |
$file->close(); | |
$file = IO::File->new(">$xsltfile") or die "unable to write-open temp file '$xsltfile'"; | |
print $file $xslt; | |
$file->close(); | |
my $result = `echo '$xslt' | $xsltproc $xsltfile $tempfile`; | |
print $result; | |
unlink($tempfile); | |
__DATA__ | |
<?xml version="1.0" encoding="iso-8859-1"?> | |
<!-- xml pretty printing xslt --> | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="no"/> | |
<xsl:variable name="indent_text" select="' '"/> | |
<xsl:template match="*[count(*) = 0]"> | |
<xsl:param name="indent" select="0"/> | |
<xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template> | |
<xsl:element name="{name()}"><xsl:copy-of select="@*"/><xsl:value-of select="normalize-space(.)"/></xsl:element><xsl:text> | |
</xsl:text> | |
</xsl:template> | |
<xsl:template match="*[count(*) > 0]"> | |
<xsl:param name="indent" select="0"/> | |
<xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template> | |
<xsl:element name="{name()}"><xsl:copy-of select="@*"/><xsl:text> | |
</xsl:text> | |
<xsl:apply-templates><xsl:with-param name="indent" select="$indent + 1"/></xsl:apply-templates> | |
<xsl:call-template name="indent"><xsl:with-param name="count" select="$indent"/></xsl:call-template></xsl:element><xsl:text> | |
</xsl:text> | |
</xsl:template> | |
<xsl:template name="indent"> | |
<xsl:param name="count"/> | |
<xsl:if test="$count > 0"> | |
<xsl:copy-of select="$indent_text"/> | |
<xsl:call-template name="indent"> | |
<xsl:with-param name="count" select="$count - 1"/> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> | |
</xsl:stylesheet> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment