def a_xml_attribute(cls, audit_id, audit_title, audit_group, audit_result, xml_obj: CXml, xpath: str, attr_name: str, qa_items: dict) -> list: result_dict = cls.__init_audit_dict__(audit_id, audit_title, audit_group, audit_result) if xml_obj is None: result_dict[cls.Name_Message] = 'XML对象不合法, 节点[{0}]不存在'.format( xpath) return [result_dict] element_obj = xml_obj.xpath_one(xpath) if element_obj is not None: if CXml.attr_exist(element_obj, attr_name): attr_text = CXml.get_attr(element_obj, attr_name, '') return cls.__a_check_value__( result_dict, attr_text, '属性[{0}]在XML节点[{1}.{2}]'.format(audit_title, xpath, attr_name), qa_items) else: result_dict[ cls. Name_Message] = '属性[{0}]在XML节点[{1}.{2}]未找到, 请检查修正!'.format( audit_title, xpath, attr_name) else: result_dict[ cls.Name_Message] = '属性[{0}]在XML节点[{1}]未找到, 请检查修正!'.format( audit_title, xpath) return [result_dict]
def test_element_attr_exist(self): """ 设置一个节点的文本 :return: """ xml_content = '''<root name="hello world"><element hello="中国"></element><element hello="美国"></element></root>''' xml = CXml() xml.load_xml(xml_content) element = xml.xpath('/root/element')[1] assert CXml.attr_exist(element, 'hello', False)