1

I have a XML file like:

<ns0:component xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685- 
2014" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2014 
http://www.accellera.org/XMLSchema/IPXACT/1685-2014/index.xsd">
    <ns0:vendor>spiritconsortium.org</ns0:vendor>
    <ns0:library>Leon2RTL</ns0:library>
    <ns0:name>ahbbus12</ns0:name>
<ns0:circuitFunctionDescriptions><ns0:name>PLL</ns0:name> 
</ns0:circuitFunctionDescriptions></ns0:component>

But I want a pretty form like:

<ns0:component xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685- 
    2014" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.accellera.org/XMLSchema/IPXACT/1685-2014 
    http://www.accellera.org/XMLSchema/IPXACT/1685-2014/index.xsd">
    <ns0:vendor>spiritconsortium.org</ns0:vendor>
    <ns0:library>Leon2RTL</ns0:library>
    <ns0:name>ahbbus12</ns0:name>
    <ns0:circuitFunctionDescriptions>
       <ns0:name>PLL</ns0:name> 
    </ns0:circuitFunctionDescriptions>
</ns0:component>

and I need to save this as an output file (I mean I need to save the result in another xml file not just print it). I wonder how can I do this?

Morteza
  • 19
  • 3

1 Answers1

0

Most serializers with pretty-print indentation capability will provide something resembling the element indentation you seek. See Pretty printing XML in Python since you've tagged your question with .

Some will provide attribute indentation too.

However, none will break attribute values as is happening in your desired XML because in general it is not possible to add whitespace within an attribute value and still be assured that semantics will be preserved.

You can see this in your example.

This namespace declaration (even if we're generous and repair it from your source document),

xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685-2014"

is not the same as

    xmlns:ns0="http://www.accellera.org/XMLSchema/IPXACT/1685- 
    2014"
kjhughes
  • 106,133
  • 27
  • 181
  • 240