Re: [go-nuts] Re: XML Beautifier that doesn't rewrite the document

2016-11-07 Thread Tong Sun
On Sun, Nov 6, 2016 at 5:35 PM, Tong Sun wrote:

>
> On Saturday, November 5, 2016 at 3:51:55 PM UTC-4, Tong Sun wrote:
>>
>>
>> On Saturday, November 5, 2016 at 3:42:27 PM UTC-4, Tong Sun wrote:
>>>
>>>
>>> On Sat, Nov 5, 2016 at 12:26 PM, Sam Whited  wrote:
>>>
 On Fri, Nov 4, 2016 at 4:32 PM, Tong Sun wrote:
 > How to beautify a given XML string in GO?...
>>>
>>> I guess such thing doesn't exist, but let me ask away anyway -- the
>>> following is exactly what I was looking for:
>>>
>>> from http://stackoverflow.com/questions/21117161:
>>>
>>> I like this solution, but am still in search of a Golang XML
 formatter/prettyprinter that doesn't rewrite the document (other than
 formatting whitespace). Marshalling or using the Encoder will change
 namespace declarations. For example an element like "" will
 be translated to something like '' which
 seems harmless enough except when the intent is to not alter the xml other
 than formatting. – James McGill
  Nov 12 '15
 
>>>
>>>
>>> Using Sam's above code as an example,
>>>
>>> https://play.golang.org/p/JUqQY3WpW5
>>>
>>> The above code format the following XML
>>>
>>> http://schemas.
>>> xmlsoap.org/soap/envelope/"
>>>   xmlns:ns="http://example.com/ns;>
>>>
>>>
>>>  
>>>   
>>>123
>>>John Brown
>>>   
>>>  
>>>
>>> 
>>>
>>>
>>> into this:
>>>
>>> http://schemas.xmlsoap.org/soap/envelope/;
>>> xmlns:_xmlns="xmlns" _xmlns:soapenv="http://schemas
>>> .xmlsoap.org/soap/envelope/" _xmlns:ns="http://example.com/ns;>
>>>  http://schemas.xmlsoap.org/soap/envelope/;>
>>>  http://schemas.xmlsoap.org/soap/envelope/;>
>>>   http://example.com/ns;>
>>>http://example.com/ns;>
>>> http://example.com/ns;>123
>>> http://example.com/ns; type="NCHZ">John Brown
>>>
>>>   
>>>  
>>> 
>>>
>>>
>>> I know they are the same in syntax, however they look totally different.
>>>
>>> Any way (e.g., to tweak encoding/xml) to make the beautified string look
>>> closer to the original?
>>>
>>
>> Well, I *got it working*. ..., and I'm *quite happy* with its look. I
> believe it'd be much faster than any XML decode/encode route. *Starting
> to convert the algorithm into Go code*...
>


FTR,

https://github.com/go-xmlfmt/xmlfmt/

HTH

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: XML Beautifier that doesn't rewrite the document

2016-11-06 Thread Florian Weimer
* Tong Sun:

> The dark voodoo regexp as described here works for many cases.
> http://www.perlmonks.org/?node_id=261292

In general, whitespace is significant in XML, so you need a DTD or
schema to make sure that you can make such edits without changing the
meaning of the document.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: XML Beautifier that doesn't rewrite the document

2016-11-06 Thread Tong Sun
On Sun, Nov 6, 2016 at 5:08 AM, Simon Ritchie wrote:

I have a much simpler approach which works for me.  Open the document with
> a web browser and it displays it nicely formatted.


Yeah, I believe it is doable. If Chrome and/or Firefox can do
it, beautifying XML without rewrite the document, then Go should be able to
do it too. I'll be continually looking into it...

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: XML Beautifier that doesn't rewrite the document

2016-11-05 Thread Sam Whited
On Sat, Nov 5, 2016 at 2:51 PM, Tong Sun  wrote:
> The dark voodoo regexp as described here works for many cases.
> http://www.perlmonks.org/?node_id=261292
>
> But I did found its own limits when trying more samples...

Regular expression are, well, regular. This means that they can parse
regular grammras, but can't parse context free grammars (like XML). It
is actually impossible to use a regex to do this task; it will always
be fragile, unfortunately.

If you need to deal with more complicated namespaces like this, I'm
afraid the encoding/xml package won't be able to do it either (see
https://github.com/golang/go/issues/13400 and the many other open bugs
about the issues). It's just not properly supported.

Good luck!

Best,
Sam

-- 
Sam Whited
pub 4096R/54083AE104EA7AD3

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.