Encode and decode to and from XML. Whitespace is not conserved for round trips - but the order of the fields are.
Consecutive xml nodes with the same name are assumed to be arrays.
XML content data, attributes processing instructions and directives are all created as plain fields.
This can be controlled by:
Flag
Default
Sample XML
Default Attribute Prefix will be changing in v4.30! In order to avoid name conflicts (e.g. having an attribute named "content" will create a field that clashes with the default content name of "+content") the attribute prefix will be changing to "+@".
This will affect users that have not set their own prefix and are not roundtripping XML changes.
Encoder / Decoder flag options
In addition to the above flags, there are the following xml encoder/decoder options controlled by flags:
Flag
Default
Description
See below for examples
Parse xml: simple
Notice how all the values are strings, see the next example on how you can fix that.
All values are assumed to be strings when parsing XML, but you can use the from_yaml operator on all the strings values to autoparse into the correct type.
In XML, if your array has a single item, then yq doesn't know its an array. This is how you can consistently force it to be an array. This handles the 3 scenarios of having nothing in the array, having a single item and having multiple.
Given a sample.xml file of:
<zoo><animal>cat</animal></zoo>
then
yq-oy'.zoo.animal |= ([] + .)'sample.xml
will output
zoo:animal: - cat
Parse xml: force all as an array
Given a sample.xml file of:
<zoo><thing><frog>boing</frog></thing></zoo>
then
yq-oy'.. |= [] + .'sample.xml
will output
- zoo: - thing: - frog: - boing
Parse xml: attributes
Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.
<!-- before cat --><cat><!-- in cat before --> <x>3<!-- multiline comment for x --></x><!-- before y --> <y><!-- in y before --> <d><!-- in d before -->z<!-- in d after --></d><!-- in y after --> </y><!-- in_cat_after --></cat><!-- after cat -->
then
yq-oy'.'sample.xml
will output
# before catcat:# in cat beforex:"3"# multi# line comment # for x# before yy:# in y before# in d befored:z# in d after# in y after# in_cat_after# after cat
A best effort is made, but comment positions and white space are not preserved perfectly.
Given a sample.xml file of:
<!-- before cat --><cat><!-- in cat before --> <x>3<!-- multiline comment for x --></x><!-- before y --> <y><!-- in y before --> <d><!-- in d before -->z<!-- in d after --></d><!-- in y after --> </y><!-- in_cat_after --></cat><!-- after cat -->
then
yq'.'sample.xml
will output
<!-- before cat --><cat><!-- in cat before --> <x>3<!-- multiline comment for x --></x><!-- before y --> <y><!-- in y beforein d before --> <d>z<!-- in d after --></d><!-- in y after --> </y><!-- in_cat_after --></cat><!-- after cat -->
Roundtrip: with doctype and declaration
yq parses XML proc instructions and directives into nodes. Unfortunately the underlying XML parser loses whitespace information.