ws_tools.py 文件源码

python
阅读 16 收藏 0 点赞 0 评论 0

项目:roulier 作者: akretion 项目源码 文件源码
def remove_empty_tags(xml, ouput_as_string=True):
    """Remove empty tags with xslt transformation.

    param: xml a string or a etree type
    return: unicode string or lxml.etree._XSLTResultTree
    """
    # use Jinja env for getting the path of template file
    # pkg_resouces may be an alternative, but we already
    # have Jinja
    env = Environment(
        loader=PackageLoader('roulier', 'templates'),
        extensions=['jinja2.ext.with_'])
    template = env.get_template("remove_empty_tags.xsl")
    xsl = etree.parse(open(template.filename))
    transform = etree.XSLT(xsl)

    if isinstance(xml, basestring):
        xml = etree.fromstring(xml)
    # else we asume xml is an lxml.etree
    if ouput_as_string:
        return unicode(transform(xml))
    else:
        return transform(xml)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号