Shoutout to 2?xml2?

Published: Fri 11 August 2017

In Blog.

I'd be remiss in not dedicating a blog post to this handy little utility, designed to allow Unix-heads to extend their honed command-line fu to XML. Let's demonstrate with a small example:

$ echo "/root/=something in here" | 2xml
<root>something in here</root>

2xml takes line-oriented hierarchical data and converts into into XML. 2xml has a convenient brother called xml2 which does the reverse, so, given:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "">
 xmlns="" xmlns:xlink=""
 style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision;
 image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
 width="70.5px" height="48px"
 viewBox="0 0 70.5 48"
 font-size="11pt" text-anchor="middle"
 stroke="black" stroke-miterlimit="10" stroke-width="1" fill="none">
<rect x="0.5" y="0.5" width="67.5" height="45" />
<text stroke-width="0.2pt" fill="black" x="32.285714" y="27.8125">


cat ex.svg | xml2

yields the following:

/svg/@style=shape-rendering:geometricPrecision; text-rendering:geometricPrecision;  image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd
/svg/@viewBox=0 0 70.5 48

Of course, the original SVG file that I used was generated using dpic which I have spoken about in a previous article. dpic has no facility to change the font to something else, so a utility-pair like 2xml/xml2 is welcome in doing a little XML post-processing:

cat ex.svg | xml2 | sed '\|/svg/@font|a\
' | 2xml > ex2.svg

The command-line fu above adds an xml2 —format line directly after the font size attribute specifying that a "sans" font be used overall.





Comments !