y
y
yq
Search…
Working with XML
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 and attributes are created as fields. This can be controlled by the '--xml-attribute-prefix and --xml-content-name flags - see below for examples.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Parse xml: simple

Notice how all the values are strings, see the next example on how you can fix that.
Given a sample.xml file of:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<cat>
3
<says>meow</says>
4
<legs>4</legs>
5
<cute>true</cute>
6
</cat>
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
cat:
2
says: meow
3
legs: "4"
4
cute: "true"
Copied!

Parse xml: number

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.
Given a sample.xml file of:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<cat>
3
<says>meow</says>
4
<legs>4</legs>
5
<cute>true</cute>
6
</cat>
Copied!
then
1
yq -p=xml ' (.. | select(tag == "!!str")) |= from_yaml' sample.xml
Copied!
will output
1
cat:
2
says: meow
3
legs: 4
4
cute: true
Copied!

Parse xml: array

Consecutive nodes with identical xml names are assumed to be arrays.
Given a sample.xml file of:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<animal>cat</animal>
3
<animal>goat</animal>
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
animal:
2
- cat
3
- goat
Copied!

Parse xml: attributes

Attributes are converted to fields, with the default attribute prefix '+'. Use '--xml-attribute-prefix` to set your own.
Given a sample.xml file of:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<cat legs="4">
3
<legs>7</legs>
4
</cat>
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
cat:
2
+legs: "4"
3
legs: "7"
Copied!

Parse xml: attributes with content

Content is added as a field, using the default content name of +content. Use --xml-content-name to set your own.
Given a sample.xml file of:
1
<?xml version="1.0" encoding="UTF-8"?>
2
<cat legs="4">meow</cat>
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
cat:
2
+content: meow
3
+legs: "4"
Copied!

Parse xml: custom dtd

DTD entities are ignored.
Given a sample.xml file of:
1
<?xml version="1.0"?>
2
<!DOCTYPE root [
3
<!ENTITY writer "Blah.">
4
<!ENTITY copyright "Blah">
5
]>
6
<root>
7
<item>&writer;&copyright;</item>
8
</root>
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
root:
2
item: '&writer;&copyright;'
Copied!

Parse xml: with comments

A best attempt is made to preserve comments.
Given a sample.xml file of:
1
<!-- before cat -->
2
<cat>
3
<!-- in cat before -->
4
<x>3<!-- multi
5
line comment
6
for x --></x>
7
<!-- before y -->
8
<y>
9
<!-- in y before -->
10
<d><!-- in d before -->z<!-- in d after --></d>
11
12
<!-- in y after -->
13
</y>
14
<!-- in_cat_after -->
15
</cat>
16
<!-- after cat -->
Copied!
then
1
yq -p=xml '.' sample.xml
Copied!
will output
1
# before cat
2
cat:
3
# in cat before
4
x: "3" # multi
5
# line comment
6
# for x
7
# before y
8
9
y:
10
# in y before
11
# in d before
12
d: z # in d after
13
# in y after
14
# in_cat_after
15
# after cat
Copied!

Encode xml: simple

Given a sample.yml file of:
1
cat: purrs
Copied!
then
1
yq -o=xml '.' sample.yml
Copied!
will output
1
<cat>purrs</cat>
Copied!

Encode xml: array

Given a sample.yml file of:
1
pets:
2
cat:
3
- purrs
4
- meows
Copied!
then
1
yq -o=xml '.' sample.yml
Copied!
will output
1
<pets>
2
<cat>purrs</cat>
3
<cat>meows</cat>
4
</pets>
Copied!

Encode xml: attributes

Fields with the matching xml-attribute-prefix are assumed to be attributes.
Given a sample.yml file of:
1
cat:
2
+name: tiger
3
meows: true
Copied!
then
1
yq -o=xml '.' sample.yml
Copied!
will output
1
<cat name="tiger">
2
<meows>true</meows>
3
</cat>
Copied!

Encode xml: attributes with content

Fields with the matching xml-content-name is assumed to be content.
Given a sample.yml file of:
1
cat:
2
+name: tiger
3
+content: cool
Copied!
then
1
yq -o=xml '.' sample.yml
Copied!
will output
1
<cat name="tiger">cool</cat>
Copied!

Encode xml: comments

A best attempt is made to copy comments to xml.
Given a sample.yml file of:
1
# above_cat
2
cat: # inline_cat
3
# above_array
4
array: # inline_array
5
- val1 # inline_val1
6
# above_val2
7
- val2 # inline_val2
8
# below_cat
Copied!
then
1
yq -o=xml '.' sample.yml
Copied!
will output
1
<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
2
<array>val1<!-- inline_val1 --></array>
3
<array><!-- above_val2 -->val2<!-- inline_val2 --></array>
4
</cat><!-- below_cat -->
Copied!

Round trip: with comments

A best effort is made, but comment positions and white space are not preserved perfectly.
Given a sample.xml file of:
1
<!-- before cat -->
2
<cat>
3
<!-- in cat before -->
4
<x>3<!-- multi
5
line comment
6
for x --></x>
7
<!-- before y -->
8
<y>
9
<!-- in y before -->
10
<d><!-- in d before -->z<!-- in d after --></d>
11
12
<!-- in y after -->
13
</y>
14
<!-- in_cat_after -->
15
</cat>
16
<!-- after cat -->
Copied!
then
1
yq -p=xml -o=xml '.' sample.xml
Copied!
will output
1
<!-- before cat --><cat><!-- in cat before -->
2
<x>3<!-- multi
3
line comment
4
for x --></x><!-- before y -->
5
<y><!-- in y before
6
in d before -->
7
<d>z<!-- in d after --></d><!-- in y after -->
8
</y><!-- in_cat_after -->
9
</cat><!-- after cat -->
Copied!