sdv.utils Module

sdv.utils.get_etree_root(doc)

Returns an instance of lxml.etree._Element for the given doc input.

Parameters:doc – The input XML document. Can be an instance of lxml.etree._Element, lxml.etree._ElementTree, a file-like object, or a string filename.
Returns:An lxml.etree._Element instance for doc.
Raises:ValidationError – If doc cannot be found or is not a well-formed XML document.
sdv.utils.get_namespace(node)

Returns the namespace for which node falls under.

Parameters:node – An etree node.
sdv.utils.get_schemaloc_pairs(node)

Parses the xsi:schemaLocation attribute on node.

Returns:A list of (ns, schemaLocation) tuples for the node.
Raises:KeyError – If node does not have an xsi:schemaLocation attribute.
sdv.utils.get_target_ns(doc)

Returns the value of the targetNamespace attribute found on doc.

Returns:

The value of the targetNamespace attribute found at the root of doc.

Raises:
  • KeyError – If doc does not contain a targetNamespace attribute.
  • ValidationError – If doc cannot be found or is not a well-formed XML document.
sdv.utils.get_type_ns(doc, typename)

Returns the namespace associated with the xsi:type typename found in the XML document doc.

Parameters:
  • doc – An XML document. This can be a filename, file-like object, etree._Element, or etree._ElementTree instance.
  • typename – The xsi:type value for a given vocabulary instance.
sdv.utils.get_xml_files(files, recursive=False)

Returns a list of files to validate from files. If a member of files is a directory, its children with a .xml extension will be added to the return value.

Parameters:
  • files – A list of file paths and/or directory paths.
  • recursive – If true, this will descend into any subdirectories of input directories.
Returns:

A list of file paths to validate.

sdv.utils.get_xml_parser(encoding=None)

Returns an etree.ETCompatXMLParser instance.

sdv.utils.ignored(*args, **kwds)

Allows you to ignore exceptions cleanly using context managers. This exists in Python 3.

sdv.utils.is_cybox(doc)

Attempts to determine if the input doc is a CybOX XML instance document. If the root-level element falls under a namespace which starts with http://cybox.mitre.org, this will return True.

sdv.utils.is_stix(doc)

Attempts to determine if the input doc is a STIX XML instance document. If the root-level element falls under a namespace which starts with http://stix.mitre.org, this will return True.

sdv.utils.is_version_equal(x, y)

Attempts to determine if the x amd y version numbers are semantically equivalent.

Examples

The version strings “2.1.0” and “2.1” represent semantically equivalent versions, despite not being equal strings.

Parameters:
  • x – A string version number. Ex: ‘2.1.0’
  • y – A string version number. Ex: ‘2.1’
sdv.utils.list_xml_files(directory, recursive=False)

Returns a list of file paths for XML files contained within dir_.

Parameters:
  • dir_ – A path to a directory.
  • recursive – If True, this function will descend into all subdirectories.
Returns:

A list of XML file paths directly under dir_.