Re: [xsl] XML pretty printer

Subject: Re: [xsl] XML pretty printer
From: TW <zupftom@xxxxxxxxxxxxxx>
Date: Tue, 27 Sep 2011 08:22:01 +0200
This works for your example, but will most certainly need more
sophistication.  For example, it ignores text nodes entirely, doesn't
take care of proper entity escaping in attributes, is ignorant of
namespaces etc.


<?xml version="1.0" encoding="UTF-8"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform"; version="1.0">
  <output method="text"/>
  <template match="/">
    <apply-templates/>
  </template>

  <template match="*">
    <param name="indent" select="''"/>
    <value-of select="concat($indent,'&lt;',local-name(),'&#10;')"/>
    <apply-templates select="@*">
      <with-param name="indent" select="concat($indent,'  ')"/>
    </apply-templates>
    <apply-templates select="." mode="writeChildElementsAndClose">
      <with-param name="indent" select="$indent"/>
    </apply-templates>
  </template>

  <template match="*[*]" mode="writeChildElementsAndClose">
    <param name="indent"/>
    <value-of select="concat($indent,'&gt;','&#10;')"/>
    <apply-templates select="*">
      <with-param name="indent" select="concat($indent,'  ')"/>
    </apply-templates>
    <value-of select="concat($indent,'&lt;/',local-name(),'&gt;&#10;')"/>
  </template>

  <template match="*" mode="writeChildElementsAndClose">
    <param name="indent"/>
    <value-of select="concat($indent,'/&gt;','&#10;')"/>
 </template>

  <template match="@*">
    <param name="indent"/>
    <variable name="quot">
      <text>"</text>
    </variable>
    <value-of
select="concat($indent,local-name(),'=',$quot,string(),$quot,'&#10;')"/>
  </template>
</stylesheet>




2011/9/26 John Christopher <john.christopher1100@xxxxxxxxx>:
> I am looking for a stylesheet (or free software
> that runs from the command line on linux) that
> will take the following input:
>
> <mydata attrib1="hello" attrib2="world"
> attrib3="yes"
>>
> <person name="Fred" country="US" />
> <person name="Karen" country="DE" />
> </mydata>
>
>
> and pretty prints it like this (each attribute
> printed on a separate line beneath the element
> and indented):
>
>
> <mydata
>    attrib1="hello"
>    attrib2="world"
>    attrib3="yes"
>>
>    <person
>        name="Fred"
>        country="US"
>    />
>    <person
>        name="Sylvia"
>        country="DE"
>    />
> </mydata>
>
> Any ideas?  Thanks for your help.

Current Thread