Skip to main content

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

Project description

https://travis-ci.org/theatlantic/django-xml.svg?branch=master

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

Note

  • 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

Installation

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://github.com/theatlantic/django-xml#egg=django-xml

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

python setup.py develop

Example

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)")

    @xmlmodels.lxml_extension
    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])

    @xmlmodels.lxml_extension
    def sqrt(self, context, number_nodes):
        sqrts = []
        for number_node in number_nodes:
            number = getattr(number_node, 'text', number_node)
            sqrts.append(repr(math.sqrt(int(number))))
        return sqrts

def main():
    numbers_xml = u"""
    <numbers>
        <num>1</num>
        <num>2</num>
        <num>3</num>
        <num>4</num>
        <num>5</num>
        <num>6</num>
        <num>7</num>
    </numbers>"""

    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__':
    main()

Advanced Example

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

API Documentation

Read API documentation on github

Project details


Download files

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

Source Distribution

django-xml-2.1.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

django_xml-2.1.0-py2.py3-none-any.whl (28.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-xml-2.1.0.tar.gz.

File metadata

  • Download URL: django-xml-2.1.0.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for django-xml-2.1.0.tar.gz
Algorithm Hash digest
SHA256 f5b70a6e6f48a5317fe356950983f7e284ed2ba62151fd971a45e67e26b281cf
MD5 bb21f807ca45c42f1e72ef752f81cbcd
BLAKE2b-256 d9955f9a7054e1d9f099032164455d21d586bc6f4f955d1299a517a9094caa69

See more details on using hashes here.

File details

Details for the file django_xml-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_xml-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.3

File hashes

Hashes for django_xml-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c62d1399c53bd069e5001c03b32472ad9129d871bec62ca77e294a2e4ce6e371
MD5 c95c7a4a4e7a3ea51d2766edc79f83a8
BLAKE2b-256 658ea2795a43680dbc69161dbf3d0139e509e40ff02bf2a97d47903c9e03627d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page