Skip to main content

Provides an abstraction to lxml's XPath and XSLT functionality in a manner resembling django database models

Project description

django-xml is a python module which provides an abstraction to lxml’s XPath and XSLT functionality in a manner resembling django database models.


  • Version 2.0 drops support for Django < 1.11
  • Version 2.0.1 drops support for Python 3.4
  • Version 3.0 will drop support for Python < 3.5


To install the latest stable release of django-xml, use pip or easy_install

pip install django-xml
easy_install django-xml

For the latest development version, install from source with pip:

pip install -e git+git://

If the source is already checked out, install via setuptools:

python develop


import math
from djxml import xmlmodels

class NumbersExample(xmlmodels.XmlModel):

    class Meta:
        extension_ns_uri = "urn:local:number-functions"
        namespaces = {"fn": extension_ns_uri,}

    all_numbers  = xmlmodels.XPathIntegerListField("//num")
    even_numbers = xmlmodels.XPathIntegerListField("//num[fn:is_even(.)]")
    sqrt_numbers = xmlmodels.XPathFloatListField("fn:sqrt(//num)")

    def is_even(self, context, number_nodes):
        numbers = [getattr(n, 'text', n) for n in number_nodes]
        return all([bool(int(num) % 2 == 0) for num in numbers])

    def sqrt(self, context, number_nodes):
        sqrts = []
        for number_node in number_nodes:
            number = getattr(number_node, 'text', number_node)
        return sqrts

def main():
    numbers_xml = u"""

    example = NumbersExample.create_from_string(numbers_xml)

    print "all_numbers  = %r" % example.all_numbers
    print "even_numbers = %r" % example.even_numbers
    print "sqrt_numbers = [%s]" % ', '.join(['%.3f' % n for n in example.sqrt_numbers])
    # all_numbers  = [1, 2, 3, 4, 5, 6, 7]
    # even_numbers = [2, 4, 6]
    # sqrt_numbers = [1.000, 1.414, 1.732, 2.000, 2.236, 2.449, 2.646]

if __name__ == '__main__':

Advanced Example

An example of django-xml usage which includes XsltField and @lxml_extension methods can be found here.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-xml, version 2.1.0
Filename, size File type Python version Upload date Hashes
Filename, size django_xml-2.1.0-py2.py3-none-any.whl (28.8 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django-xml-2.1.0.tar.gz (24.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page