XML Plugin

The plugin provides a set of actions for transformation and validation of XML data.

Installation

Example 1. build.gradle
implementation(group: 'org.vividus', name: 'vividus-plugin-xml', version: '0.4.8')

Steps

Save data by XPath

Saves XML data by XPath to a variable.

When I save data found by xpath `$xpath` in XML `$xml` to $scopes variable `$variableName`
Example 2. Save message body
When I save data found by xpath `/message/body` in XML `
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <to>Bob</to>
  <from>Alice</from>
  <heading>Reminder</heading>
  <body>Don't forget to fill TJ gaps for this week</body>
</message>` to scenario variable `body`
Then `${body}` is equal to `Don't forget to fill TJ gaps for this week`

Transform XML document

Performs transformation of XML document using XSLT and saves the result to a variable.

When I transform XML `$xml` with `$xslt` and save result to $scopes variable `$variableName`
Example 3. Transform CD catalog into list of artists
When I transform XML `
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <price>8.20</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <price>9.90</price>
        <year>1988</year>
  </cd>
</catalog>
` with `
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="catalog/cd">
            <xsl:value-of select="artist"/>,
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
` and save result to scenario variable `artists`
Then `${artists}` is equal to `* Joe Cocker * Bob Dylan * Bonnie Tyler`

Check element existence

Checks if XML document contains element by XPath.

Then XML `$xml` contains element by xpath `$xpath`
Example 4. Check a message contains heading element
Then XML `
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <to>Bob</to>
  <from>Alice</from>
  <heading>Reminder</heading>
  <body>Don't forget to fill TJ gaps for this week</body>
</message>
` contains element by xpath `/message/heading`

Compare XML documents

Checks if XML document is equal to expected XML document.

Then XML `$xml` is equal to `$expectedXml`
Example 5. Compare messages
Then XML `
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <to>Bob</to>
  <from>Alice</from>
  <heading>Reminder</heading>
  <body>Don't forget to fill TJ gaps for this week</body>
</message>
` is equal to `
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <to>Bob</to>
  <from>Alice</from>
  <heading>Reminder</heading>
  <body>Don't forget to fill TJ gaps for this week</body>
</message>
`

Validate XML document

Validates XML document against XSD Schema.

Then XML `$xml` is valid against XSD `$xsd`
Example 6. Validate shipping information
Then XML `
<?xml version="1.0" encoding="UTF-8"?>
<shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
</shipto>
` is valid against XSD `
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="shipto">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="address" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="country" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
`