0

I have the following XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-model href="../tool/rules/rules.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>

<block name="test_debug" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://my-specific-ns"
    xsi:noNamespaceSchemaLocation="../tool/rules/schema.xsd" simul="true">
</block>

And my Python3 code is the following

XmlRoot = ET.parse('test.xml').getroot() #type: ElementTree
XmlRoot.attrib

and I get the following output

{'name': 'test_debug', '{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation': '../tool/rules/schema.xsd', 'simul': 'true'}

I do not understand why there are two attributes missing.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

and

xmlns="http://my-specific-ns"

I am interested in getting the xmlns attribute. Because its the namespace used by through my XML files and it will make it easier to search for specific paths.

PS I'm using Python 3.7.

/Regards

Ephreal
  • 1,835
  • 2
  • 18
  • 35
  • `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"` and `xmlns="http://my-specific-ns"` are not "real" attributes. They are namespace declarations and do not populate `attrib`. If you can move to Python 3.8, there is the option of using wildcards for namespaces. See https://stackoverflow.com/a/62117710/407651. – mzjn Jun 15 '20 at 15:59
  • Or use lxml, which has a `nsmap` property on elements: See https://stackoverflow.com/a/21569520/407651 – mzjn Jun 15 '20 at 16:07

0 Answers0