GithubHelp home page GithubHelp logo

ruledxml's Introduction

ruledxml

Given an XML file. Transform it into another XML file using a rules file. A rules file reads some element, process them and returns a new value which is written to an XML path. Rules are inspired by Business Rule Engines such as Intellect.

Installation

Installation with source package:

  1. python3 setup.py install
  2. pip3 install -r requirements.txt

Installation from PyPI:

  1. pip3 install ruledxml

Run the testsuite:

  1. Start python3
  2. Run import ruledxml.tests
  3. Run ruledxml.tests.runall()

Rules

Rules are designed to be written with little technical knowledge. However, not all transformations (comparable to XSLT) are possible. If elements occur multiple times in the source XML, the capabilities are limited.

Rules are written in the python programming language:

@source("/root/body/header")
@destination("/html/body/article/h1")
def ruleFirstHeader(header):
    return header + "!"

The first element at XPath /root/body/header is read, its text content is looked up and passed as argument header to the function ruleFirstHeader.

If the source element does not exist, an empty string is supplied. Be aware that source parameters are always strings.

The rule returns the original header appended with an exclamation mark. The return value is written to the destination XPath /html/body/article/h1. All non-existing elements will be created. If an element already exists, the first match is taken.

Required elements

A source of errors in that design is when elements do not exist and an empty string is supplied as argument. This is a silent error.

You can specify required elements in a variable input_required which is interpreted before the actual processing starts:

input_required = [
    "/root/body/header"
]

If the element /root/body/header does not exist in the source XML file, a ValueError is thrown.

Non-empty elements

Similarly to required elements, we have non-empty elements. Paths mentioned in the input_nonempty variable are required to yield nonempty values. Otherwise an error is thrown and processing aborted.

Implementation

A little bit of lxml and lots of decorator magic ;)

License

3-claused BSD license. Hence you can use the software for whatever you like, if you want to mention my name in your software product, you need to ask me.

cheers, meisterluk <[email protected]>

ruledxml's People

Contributors

meisterluk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.