harvester/utils/xml_data_extraction.py
def xml_extract_text_values(s):
    """
    Extract all the text found in an xml, along with its path.

    :param s: The XML data, as string
    :return: a dictionary mapping ``{path: [values ...]}``
    """

    tree = lxml.etree.fromstring(s)
    found_data = defaultdict(list)

    def _get_tag_name(elem):
        localname = lxml.etree.QName(elem.tag).localname
        if elem.prefix is not None:
            return ':'.join((elem.prefix, localname))
        return localname

    def _get_tag_path(elem):
        path = [_get_tag_name(x)
                for x in reversed(list(elem.iterancestors()))]
        path.append(_get_tag_name(elem))
        return '/' + '/'.join(path)

    # for elem in tree.xpath('//*[text()]'):
    for elem in tree.xpath('//*'):
        if elem.text is None:
            continue

        text = ' '.join(elem.text.split()).strip()

        if not text:
            continue  # was just garbage..

        path = _get_tag_path(elem)
        found_data[path].append(text)

    return dict(found_data)

    Extract all the text found in an xml, along with its path.

    :param s: The XML data, as string
    :return: a dictionary mapping ``{path: [values ...]}``
    
Extract all the text found in an xml, along with its path.
Extract all the text found in an xml along with its path .
def xml_extract_text_values(s):
    tree = lxml.etree.fromstring(s)
    found_data = defaultdict(list)
    def _get_tag_name(elem):
        localname = lxml.etree.QName(elem.tag).localname
        if elem.prefix is not None:
            return ':'.join((elem.prefix, localname))
        return localname
    def _get_tag_path(elem):
        path = [_get_tag_name(x)
                for x in reversed(list(elem.iterancestors()))]
        path.append(_get_tag_name(elem))
        return '/' + '/'.join(path)
    for elem in tree.xpath('//*'):
        if elem.text is None:
            continue
        text = ' '.join(elem.text.split()).strip()
        if not text:
            continue  
        path = _get_tag_path(elem)
        found_data[path].append(text)
    return dict(found_data)
def xml_extract_text_values ( s ) : tree = lxml . etree . fromstring ( s ) found_data = defaultdict ( list ) def _get_tag_name ( elem ) : localname = lxml . etree . QName ( elem . tag ) . localname if elem . prefix is not None : return ':' . join ( ( elem . prefix , localname ) ) return localname def _get_tag_path ( elem ) : path = [ _get_tag_name ( x ) for x in reversed ( list ( elem . iterancestors ( ) ) ) ] path . append ( _get_tag_name ( elem ) ) return '/' + '/' . join ( path ) for elem in tree . xpath ( '//*' ) : if elem . text is None : continue text = ' ' . join ( elem . text . split ( ) ) . strip ( ) if not text : continue path = _get_tag_path ( elem ) found_data [ path ] . append ( text ) return dict ( found_data )
Extract all the text found in an xml, along with its path.
Extract all the text found in an xml along with its path .
"""
    Extract all the text found in an xml, along with its path.

    :param s: The XML data, as string
    :return: a dictionary mapping ``{path: [values ...]}``
    """ # for elem in tree.xpath('//*[text()]'): # was just garbage..
a dictionary mapping ``{path: [values ...]}``
a dictionary mapping ` ` { path : [ values ... ] } ` `
s
The XML data, as string
The XML data as string