Format XML String to Certain Line Length and Indentation

Format XML strings to a certain line length and indentation using the xml:prettyPrint() function

Query

flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0{Conditional} result{{Result Set}} repo --> 0 0 --> result
logscale
formattedXml := xml:prettyPrint(field=payload, step=4, width=100)

Introduction

The xml:prettyPrint() function can be used to format the XML in the output string (the filtered data) for improved readability.

It is recommended to apply xml:prettyPrint() after filtering your data at the end of the query. This prevents unnecessary formatting of data that will be discarded.

In this example, the xml:prettyPrint() function is used with specified step and width parameters to format the XML content in the payload field (instead of default @rawstring field).

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 0{Conditional} result{{Result Set}} repo --> 0 0 --> result style 0 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    formattedXml := xml:prettyPrint(field=payload, step=4, width=100)

    Formats XML in the field payload to a max line length of 100 and indent by 4 spaces, and returns the formatted XML result in a new field named formattedXml.

    Defining a max line length of 100 characters using the width parameter makes it easier to read (allows longer lines before wrapping).

    Larger indentation (4 spaces) provides more visual separation between XML levels.

  3. Event Result set.

Summary and Results

The query is used to format values of a specific field into valid XML of a certain line length and indentation. Defining a max line length makes it easier to read.

"Pretty printing" XML can be an expensive operation and it is recommended only to perform this after filtering the data (at the end of the query), so that only filtered data is formatted.

The xml:prettyPrint() function formats an XML field for improved readability.