i have a xml as below.
<!DOCTYPE parent [
<!ENTITY entity1 "value">
]>
<main>
<parent attr1="str1"
attr2="str2">
<child childattr1="str3"
childattr2="&entity1;" />
<child childattr1="str4"
childattr2="&entity1;" />
</parent>
</main>
I need to indent the child elements, leaving the rest of the xml file as it is (i.e the dtd section and entities should not be removed, and attributes should be on new lines). The xml should finally look like:
<!DOCTYPE parent [
<!ENTITY entity1 "value">
]>
<main>
<parent attr1="str1"
attr2="str2">
<child childattr1="str3"
childattr2="&entity1;" />
<child childattr1="str4"
childattr2="&entity1;" />
</parent>
</main>
I have tried using xmllint
and tidy
. xmllint
is indenting child elements but its not keeping attributes in new lines. tidy
on the other hand has an option to keep attributes in new lines but it is unable to indent child elements. I have tried using perl regex as well. This could probably be done with XSLT, i am however not well-versed with it.
Seems to me that XML::Twig
's xml_pp
utility does almost what you want with its indented_a
option:
$ xml_pp -s indented_a foo.xml
<!DOCTYPE parent [
<!ENTITY entity1 "value">
]>
<main>
<parent
attr1="str1"
attr2="str2">
<child
childattr1="str3"
childattr2="&entity1;"
/>
<child
childattr1="str4"
childattr2="&entity1;"
/>
</parent>
</main>