Skip to content

xml

The xml module exposes the same interface as json (isEqual, isEqualFile, isEqualFiles, compare) but accepts XML strings as input. Both strings are parsed into objects before comparison.

isEqual

Signature

javascript
xml.isEqual(xml1, xml2, options)

Description

Check if two XML strings are semantically equal. Both strings are parsed into objects and compared using deep equality. Throws a TypeError if either argument is not a non-empty string.

Parameters

NameTypeRequiredDescription
xml1stringyesThe first XML string
xml2stringyesThe second XML string
optionsobjectnoComparison options
options.parserobjectnoAdditional options passed to the XML parser

Returns

TypeDescription
booleanTrue if both XML strings are semantically equal

Examples

javascript
xml.isEqual('Alice', '  Alice  ')
// true
xml.isEqual('1', '2')
// false
xml.isEqual('1', '1')
// false — attributes compared

isEqualFile

Signature

javascript
xml.isEqualFile(xml, xmlFilePath, options)

Description

Check if an XML string is semantically equal to the content of an XML file. Throws a TypeError if either argument is not a non-empty string.

Parameters

NameTypeRequiredDescription
xmlstringyesThe XML string to compare
xmlFilePathstringyesPath to the XML file
optionsobjectnoComparison options (see isEqual)

Returns

TypeDescription
booleanTrue if the XML string match the file content

Examples

javascript
xml.isEqualFile('Alice', './fixtures/expected.xml')
// true or false

isEqualFiles

Signature

javascript
xml.isEqualFiles(xmlFilePath1, xmlFilePath2, options)

Description

Check if two XML files are semantically equal. Throws a TypeError if either argument is not a non-empty string.

Parameters

NameTypeRequiredDescription
xmlFilePath1stringyesPath to the first XML file
xmlFilePath2stringyesPath to the second XML file
optionsobjectnoComparison options (see isEqual)

Returns

TypeDescription
booleanTrue if both files are semantically equal

Examples

javascript
xml.isEqualFiles('./fixtures/actual.xml', './fixtures/expected.xml')
// true or false

compare

Signature

javascript
xml.compare(xml1, xml2, options)

Description

Compare two XML strings and return a detailed diff. Both strings are parsed into objects before comparison. Throws a TypeError if either argument is not a non-empty string.

Parameters

NameTypeRequiredDescription
xml1stringyesThe first XML string
xml2stringyesThe second XML string
optionsobjectnoComparison options (see isEqual)

Returns

TypeDescription
objectAn object with isEqual (boolean) and differences (object with missing, extra, updated)

Examples

javascript
xml.compare('', '')
// { isEqual: true, differences: { missing: [], extra: [], updated: [] } }

xml.compare('1', '2')
// { isEqual: false, differences: { missing: [], extra: [], updated: [{ path: 'root.count', oldValue: '1', newValue: '2' }] } }

xml.compare('1', '1')
// { isEqual: false, differences: { missing: ['root.a'], extra: ['root.b'], updated: [] } }