pythonxmlxpathxqueryprocessing-instruction

How to deal with "<?" annotation in xml file with Python


Somebody know how to deal with this kind of XML annotation with Python, it's my first time I saw this.

&lt;?link id="752760" resource-uuid="UUID-9f0575a3-1847-1cde-fd35-f18014fdecf3" resource-id="570935" resource-type="fork" type="ResourceLink"?&gt;

I need to query this kind of 'element' to get the resource-uuid value.


Solution

  • Thanks to everybody, I learned about the processing instruction and with this, I did a research about how to deal with it, bellow I let my scratch if anybody would need it:

    from lxml import etree
    
    ...
    
    file = 'path/to/file.xml'
    tree = etree.parse(file)
    result = tree.xpath('//processing-instruction("link")')
    for pi in result:
        # Each pi is a processing instruction tagged as 'link'
        if pi.get('type').__str__() == 'ResourceImport':
            # PI with type = ResourceImport
            print pi.text # Check the text of tis PI
    

    Using lxml library is easy to get the processing instructions using XPath.

    I hope this code snippet helps to people who get here because of this question.