f



Finding best way to pretty print XML files

Havn't found best soultion to pretty print XML:

REXML: It has pp functionality, but it wrap elements like <a>content</a>
to
<a>
content
</a>

Nokogiri: there is a approch to pp, see ex:

# Pretty print XML
require "nokogiri"

def write(buffer)

  xsl =<<XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:strip-space elements="*"/>
<xsl:template match="/">
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
XSL

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

But nokogiri has another problem, which unwrap CDATA.

Any better solution? Thanks. And apologize for my poor english.

-- 
Posted via http://www.ruby-forum.com/.

0
Pk
1/11/2011 7:35:56 AM
comp.lang.ruby 48877 articles. 0 followers. Post Follow

3 Replies
661 Views

Similar Articles

[PageSpeed] 10

0 resp.  ToT

-- 
Posted via http://www.ruby-forum.com/.

0
Pk
1/12/2011 12:53:30 AM
Sorry if you haven't got any help,

could you provide more informations to the output you want ?

There are many ways to indent XML code. Maybe you don't want to wrap lines
with <a>content</a>, but what if the content is longer ? Do you have a
column limit ?

In your place, I would take the pp code of nokogiri and tweak it until it
gives me what I want.

Hope that help,
   zimbatm




2011/1/12 Pk Z. <k@z-pk.com>:
> 0 resp. =C2=A0ToT
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

0
Jonas
1/12/2011 11:18:50 PM
[Note:  parts of this message were removed to make it a legal post.]

In my experience, everyone has a different definition of "pretty printing".
If you want to get more specific about what you'd like to see, perhaps
people can give you a more specific answer.

On Tue, Jan 11, 2011 at 7:53 PM, Pk Z. <k@z-pk.com> wrote:

> 0 resp.  ToT
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

0
Mike
1/13/2011 12:05:12 AM
Reply: