Sign in

    xmlfmt

    An XML formatter written in MoonBit, ported from python xmlformatter.

    xml
    formatter
    pretty-print
    cli
    Download zip
    Author
    Version
    0.1.2
    License
    Apache-2.0
    Last updated
    13 hours ago
    Downloads
    3

    Dependencies

    #xmlfmt

    An XML formatter written in MoonBit, ported from python xmlformatter (v0.2.9).

    #Features

    • Pretty-print XML with configurable indentation
    • Compress/minify XML output
    • Self-closing tags
    • Whitespace correction and normalization
    • Preserve whitespace in specified elements
    • Preserve or sort attribute order
    • Encode attribute values
    • CDATA, comments, processing instructions, DOCTYPE support
    • CLI tool with file/stdin support

    #Installation

    #As a library (MoonBit)

    moon add kupas/xmlfmt

    #As a CLI (from source)

    moon install kupas/xmlfmt/cmd/xmlfmt

    This builds from source using the MoonBit toolchain and installs the xmlfmt binary to ~/.moon/bin.

    #As a CLI (prebuilt binary)

    curl -LsSf https://raw.githubusercontent.com/tomasky/xmlfmt/main/install.sh | bash

    #npm

    npm install -g @tomasko/xmlfmt # or pnpm install -g @tomasko/xmlfmt

    #Usage

    #Library

    import "kupas/xmlfmt"

    fn main {
    let xml = "<root><child>text</child></root>"
    let result = @xmlfmt.format(xml, @xmlfmt.default_options())
    println(result)
    }

    #CLI

    moon install kupas/xmlfmt/cmd/xmlfmt xmlfmt input.xml xmlfmt --compress input.xml xmlfmt --indent 4 input.xml

    #Options

    OptionShortDefaultDescription
    --indent <num>-i2Number of indent characters per level
    --indent-char <char>-IspaceIndentation character
    --compress-cfalseMinify output: remove all unnecessary whitespace, single line
    --selfclose-sfalseUse self-closing tags for empty elements (<empty/>)
    --selfclose-space-SfalseAdd space before /> (<empty />)
    --disable-inlineformatting-lfalseDisable inline formatting (see below)
    --disable-correction-CfalseDisable whitespace correction
    --blanks-bfalsePreserve blank lines (collapse multiple into one)
    --eof-newline-nfalseAdd newline at end of file
    --preserve <elems>-pnoneComma-separated elements to preserve whitespace in
    --preserve-attributes-afalsePreserve original attribute order (default: sort)
    --encode-attributes-EfalseEncode > and ' in attribute values
    --output <file>-ononeWrite output to file
    --infile <file>-fnoneInput file
    --overwrite-wfalseOverwrite input file in place

    #Pretty Output (default)

    By default, xmlfmt produces pretty-printed output with indentation:

    <root> <child>text</child> <nested> <deep>value</deep> </nested> </root>

    #Inline Formatting

    By default, elements containing only text stay on a single line: <a>abc</a>. Elements containing child elements are expanded. Use --disable-inlineformatting (-l) to expand text-only elements too:

    <!-- default (inline) --> <root> <name>John</name> <items> <item>1</item> <item>2</item> </items> </root> <!-- with -l (disabled inline) --> <root> <name> John </name> <items> <item> 1 </item> </items> </root>

    #Compress Mode

    --compress (-c) removes all unnecessary whitespace and produces single-line output: <root><child>text</child></root>. Useful for minification.

    #Self-Closing Tags

    --selfclose (-s) converts empty elements to self-closing form:

    <!-- input: <root><empty></empty></root> --> <!-- output: <root><empty/></root> -->

    Add --selfclose-space (-S) for a space before />: <empty />.

    #Whitespace Correction

    By default, xmlfmt normalizes whitespace in text nodes: collapses multiple spaces, converts tabs/newlines to spaces, and trims leading/trailing spaces adjacent to tags. Use --disable-correction (-C) to preserve original whitespace.

    #Preserve Whitespace

    Use --preserve (-p) to keep whitespace untouched in specific elements and their descendants (e.g., <pre>, <code>, <script>):

    xmlfmt -p "pre,code,script" input.xml

    #Attribute Handling

    • By default, attributes are sorted alphabetically
    • --preserve-attributes (-a): keep original order
    • --encode-attributes (-E): also encode > and ' (default only encodes &, <, ")

    #Blank Lines

    --blanks (-b): preserve blank lines (multiple blank lines collapse to one).

    #EOF Newline

    --eof-newline (-n): ensure file ends with a newline character.

    #License

    Apache-2.0

    XmlOptions

    pub(all) struct XmlOptions {
    indent : Int
    indent_char : String
    compress : Bool
    selfclose : Bool
    selfclose_space : Bool
    inline : Bool
    correct : Bool
    blanks : Bool
    eof_newline : Bool
    preserve : Array[String]
    preserve_attributes : Bool
    encode_attributes : Bool
    }

    default_options

    fn default_options() -> XmlOptions

    format

    fn format(xml : String, options : XmlOptions) -> String

    format_default

    fn format_default(xml : String) -> String

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io