Skip to content

Latest commit

 

History

History
86 lines (76 loc) · 1.68 KB

7.PITags.md

File metadata and controls

86 lines (76 loc) · 1.68 KB

Processing Instructions are supported by Fast XML parser. However, they're treated as normal tags from parsing perpective. Hence, following properties are required to be set for attributes processing;

ignoreAttributes: false,
allowBooleanAttributes: true

Here is an example;

Input

<?xml version="1.0"?>
<?textinfo whitespace is allowed ?>

<h1></h1>

Code to process PI tags

const options = {
    ignoreAttributes: false,
    format: true,
    preserveOrder: true,
    allowBooleanAttributes: true
};
const parser = new XMLParser(options);
let result = parser.parse(xmlData);

// const builder = new XMLBuilder(options);
// const output = builder.build(result);

JS ordered object

[
    {
        "?xml": [
            {
                "#text": ""
            }
        ],
        ":@": {
            "@_version": "1.0"
        }
    },
    {
        "?textinfo": [
            {
                "#text": ""
            }
        ],
        ":@": {
            "@_whitespace": true,
            "@_is": true,
            "@_allowed": true
        }
    },
    {
        "h1": []
    }
]

Please note that

  • #text is always empty to keep it consistent with other parsed properties.
  • PI tag name starts with '?'
  • attributes are parsed in the same way they are parsed for other normal tags

When you setup builder to parse the above JS ordered object

const options = {
    ignoreAttributes: false,
    preserveOrder: true,
    allowBooleanAttributes: true,
    suppressBooleanAttributes: true
};
const builder = new XMLBuilder(options);
const output = builder.build(result);

Output

<?xml   version="1.0"?>
<?textinfo   whitespace is allowed?>
<h1></h1>