xsl-list
[Top] [All Lists]

Re: [xsl] XML pretty printer

2011-09-27 01:22:13
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(_dot_)christopher1100(_at_)yahoo(_dot_)com>:
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.


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>