diff --git a/cyclonedx/output/schema.py b/cyclonedx/output/schema.py index f36dc336..a72b15ec 100644 --- a/cyclonedx/output/schema.py +++ b/cyclonedx/output/schema.py @@ -65,6 +65,9 @@ def component_supports_bom_ref_attribute(self) -> bool: def component_supports_mime_type_attribute(self) -> bool: return True + def component_supports_licenses_expression(self) -> bool: + return True + def component_version_optional(self) -> bool: return False @@ -230,6 +233,9 @@ def component_supports_author(self) -> bool: def component_supports_bom_ref_attribute(self) -> bool: return False + def component_supports_licenses_expression(self) -> bool: + return False + def component_supports_mime_type_attribute(self) -> bool: return False diff --git a/cyclonedx/output/xml.py b/cyclonedx/output/xml.py index 5d784cb9..79516850 100644 --- a/cyclonedx/output/xml.py +++ b/cyclonedx/output/xml.py @@ -159,6 +159,7 @@ def _add_component_element(self, component: Component) -> ElementTree.Element: # licenses if component.licenses: + license_output: bool = False licenses_e = ElementTree.SubElement(component_element, 'licenses') for license in component.licenses: if license.license: @@ -177,8 +178,14 @@ def _add_component_element(self, component: Component) -> ElementTree.Element: license_text_e_attrs).text = license.license.text.content ElementTree.SubElement(license_e, 'text').text = license.license.id + license_output = True else: - ElementTree.SubElement(licenses_e, 'expression').text = license.expression + if self.component_supports_licenses_expression(): + ElementTree.SubElement(licenses_e, 'expression').text = license.expression + license_output = True + + if not license_output: + component_element.remove(licenses_e) # cpe if component.cpe: