2017-08-31 07:56:14 +00:00
|
|
|
---
|
|
|
|
title: About the xml Resource
|
|
|
|
---
|
|
|
|
|
|
|
|
# xml
|
|
|
|
|
|
|
|
Use the `xml` InSpec audit resource to test data in an XML file.
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
|
|
|
|
2017-08-31 07:56:14 +00:00
|
|
|
## Syntax
|
|
|
|
|
|
|
|
An `xml` resource block declares the data to be tested. Assume the following XML file:
|
|
|
|
|
|
|
|
<root>
|
|
|
|
<name>hello</name>
|
|
|
|
<meta>
|
|
|
|
<creator>John Doe</creator>
|
|
|
|
</meta>
|
|
|
|
<array>
|
|
|
|
<element>one</element>
|
|
|
|
<element>two</element>
|
|
|
|
</array>
|
2018-01-16 22:26:39 +00:00
|
|
|
<array>
|
|
|
|
<element value="one"></element>
|
|
|
|
<element value="two"></element>
|
|
|
|
</array>
|
2017-08-31 07:56:14 +00:00
|
|
|
</root>
|
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
This file can be queried for elements using:
|
2017-08-31 07:56:14 +00:00
|
|
|
|
|
|
|
describe xml('/path/to/name.xml') do
|
|
|
|
its('root/name') { should eq ['hello'] }
|
|
|
|
its('root/meta/creator') { should eq ['John Doe'] }
|
2017-12-11 20:07:55 +00:00
|
|
|
its('root/array[2]/element') { should eq ['two'] }
|
2017-08-31 07:56:14 +00:00
|
|
|
end
|
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
This file can be queried for attributes using:
|
|
|
|
|
|
|
|
describe xml('/path/to/name.xml') do
|
|
|
|
its('root/array[2]/element/@value') { should eq ['one', 'two'] }
|
|
|
|
its('root/array[2]/element/attribute::value') { should eq ['one', 'two'] }
|
|
|
|
its('root/array[2]/element[2]/attribute::value') { should eq ['two'] }
|
|
|
|
end
|
|
|
|
|
2017-08-31 07:56:14 +00:00
|
|
|
where
|
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
* `root/name` and `root/array[2]/element/@value` is an XPath expression
|
2017-08-31 07:56:14 +00:00
|
|
|
* `should eq ['foo']` tests a value of `root/name` as read from an XML file versus the value declared in the test
|
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
In the above example, you see the use of `@` and `attribute::` which are both methods of fetching attributes.
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2017-12-12 15:31:50 +00:00
|
|
|
In the event the path contains an element which contains periods, the alternate syntax can be used:
|
|
|
|
|
|
|
|
its(['root/name.with.a.period']) { should cmp 'so_many_dots' }
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
## Examples
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
The following examples show how to use this InSpec audit resource.
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
### Test an AppPool's presence in an applicationHost.config file or the default site under applicationHost.sites
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
describe xml('applicationHost.config') do
|
2017-12-22 13:56:18 +00:00
|
|
|
# using the alternate syntax as described above because of the . in the key name
|
|
|
|
its(['configuration/system.applicationHost/applicationPools/add@name']) { should contain('my_pool') }
|
2017-10-03 21:35:10 +00:00
|
|
|
end
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2018-01-16 22:26:39 +00:00
|
|
|
describe xml('applicationHost.sites') do
|
|
|
|
its('site[@name="Default Web Site"]/application/virtualDirectory/@path') { should eq ['/'] }
|
|
|
|
end
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
## Matchers
|
2017-08-31 07:56:14 +00:00
|
|
|
|
2018-01-31 12:24:16 +00:00
|
|
|
For a full list of available matchers please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
|
2017-08-31 07:56:14 +00:00
|
|
|
|
|
|
|
### name
|
|
|
|
|
|
|
|
The `name` matcher tests the value of `name` as read from a JSON file versus the value declared in the test:
|
|
|
|
|
|
|
|
its('name') { should eq 'foo' }
|