[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML pretty printer

Subject: Re: XML pretty printer
From: TW <zupftom@xxxxxxxxxxxxxx>
Date: Tue, 27 Sep 2011 08:22:01 +0200
Re:  XML pretty printer
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.