Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
API responsible for validating XML structures and generate python structures
Python
Branch: master

Merge pull request #21 from jamilatta/master

Ajuste na assinatura do método serialize
latest commit e61f7d248b
@jamilatta jamilatta authored

README.md

Porteira

Build Status

Overview

API responsible for validating XML structures and generate python structures

API Usage

The instance of porteira.Schema(xsd) parses and validates the time of construction instance:


>from porteira.porteira import Schema
>sch = Schema(xsd)

Validating xml against an xsd, method: Schema.validate(xml) return boolean:


>xml = '<?xml version="1.0" encoding="UTF-8"?><root><a>bla0</a><b>bla1</b></root>'
>xsd = '<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:element name="root"><xs:complexType><xs:sequence><xs:element ref="a"/><xs:element ref="b"/></xs:sequence></xs:complexType></xs:element><xs:element name="a" type="xs:NCName"/><xs:element name="b" type="xs:NCName"/>
</xs:schema>'
>from porteira.porteira import Schema
>sch = Schema(xsd)
>sch.validate(xml)
>True

Deserialized XML to dict object, method: Schema.deserialize(xml) return dict:


>xml = '<?xml version="1.0" encoding="UTF-8"?><root><a>bla0</a><b>bla1</b></root>'
>xsd = '<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:element name="root"><xs:complexType><xs:sequence><xs:element ref="a"/><xs:element ref="b"/></xs:sequence></xs:complexType></xs:element><xs:element name="a" type="xs:NCName"/><xs:element name="b" type="xs:NCName"/>
</xs:schema>'
>from porteira.porteira import Schema
>sch = Schema(xsd)
>sch.deserialize(xml)
>OrderedDict([(u'root', OrderedDict([(u'a', u'bla0'), (u'b', u'bla1')]))])

Serialize dict to XML, method: Schema.serialize(dict) return XML:


>dict_obj = {'a': {'b': '1', 'c': '2'}}
>from porteira.porteira import Schema
>sch = Schema()
>sch.serialize(dict_obj)
><?xml version="1.0" encoding="utf-8"?>\n<a><c>2</c><b>1</b></a>

Get_validation_errors return a list of syntax and schema errors otherwise empty list, method: Schema.get_validation_errors(xml) return list:

Valid XML:


>xml = '<?xml version="1.0" encoding="UTF-8"?><root><a>bla0</a><b>bla1</b></root>'
>xsd = '<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:element name="root"><xs:complexType><xs:sequence><xs:element ref="a"/><xs:element ref="b"/></xs:sequence></xs:complexType></xs:element><xs:element name="a" type="xs:NCName"/><xs:element name="b" type="xs:NCName"/>
</xs:schema>'
>from porteira.porteira import Schema
>sch = Schema(xsd)
>sch.serialize(dict_obj)
>sch.get_validation_errors(xml)
>[]

Invalid XML:


>invalid_xml = '<?xml version="1.0" encoding="UTF-8"?><root><a>bla0</a><b>bla1</b></root>'
>xsd = '<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"><xs:element name="root"><xs:complexType><xs:sequence><xs:element ref="a"/><xs:element ref="b"/></xs:sequence></xs:complexType></xs:element><xs:element name="a" type="xs:NCName"/><xs:element name="b" type="xs:NCName"/>
</xs:schema>'
>from porteira.porteira import Schema
>sch = Schema(xsd)
>sch.serialize(dict_obj)
>sch.get_validation_errors(invalid_xml)
>[(u'PARSER', 1, 53, u'FATAL', u'ERR_TAG_NAME_MISMATCH', u'Opening and ending tag mismatch: a line 1 and unparseable')]

Errors format returned from this method:

Output Format: [(DOMIAN, LINE, COLUMN, LEVEL, TYPE_NAME, MESSAGE),]

Ex.: [(PARSER, 3, 51, FATAL, ERR_TAG_NAME_MISMATCH, Opening and ending tag mismatch: statpage line 3 and startpage), (SCHEMASV, 2, 0, ERROR, SCHEMAV_CVC_ELT_1, Element 'wizard': No matching global declaration available for the validation root)]

How to Install?


>$ pip install porteira

Something went wrong with that request. Please try again.