Format Only Valid Input XML in Output String

Format only valid input XML to valid XML in output string 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=message, strict=true)

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 to format only the valid XML in the field message in the output string.

Setting the strict paramter to true, means that only valid XML input produce a value in the output field. By default - if the strict parameter is not set - invalid input XML is copied to the output field unmodified.

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=message, strict=true)

    Formats and copies valid XML to the output field formattedXml by setting the parameter strict=true.

    When strict is set to true, only valid XML input produce a value in the output field, therefore, the field formattedXml will not be created for events with invalid XML in message.

  3. Event Result set.

Summary and Results

The query is used to "pretty print" an XML field, in this example the message field, and to validate the integrity of the XML message as it only outputs if the XML is completely valid. Only the valid input XML is formatted to valid output XML. Invalid input XML is not copied to the output field unmodified.

The query is useful in cases where XML validity is critical and to ensure XML compliance.

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