| 1 |
XML Schema interface |
|---|
| 2 |
===================== |
|---|
| 3 |
|
|---|
| 4 |
This component provides an XML schema support on top of Zope3 interfaces. |
|---|
| 5 |
It allows you to set XSD document on Python Interface definitions |
|---|
| 6 |
|
|---|
| 7 |
Let's work with this simple XSD below : |
|---|
| 8 |
|
|---|
| 9 |
""" |
|---|
| 10 |
<?xml version="1.0" encoding="UTF-8"?> |
|---|
| 11 |
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
|---|
| 12 |
<xsd:element name="title" type="xsd:string"/> |
|---|
| 13 |
<xsd:element name="description" type="xsd:string"/> |
|---|
| 14 |
</xsd:schema> |
|---|
| 15 |
""" |
|---|
| 16 |
|
|---|
| 17 |
It defines two string fields : title and description |
|---|
| 18 |
|
|---|
| 19 |
First, let's register the XML Schema document through ZCML using the |
|---|
| 20 |
dedicated directive on the zope namespace : |
|---|
| 21 |
|
|---|
| 22 |
>>> from zope.xmlschema.testing import registerDirective |
|---|
| 23 |
|
|---|
| 24 |
>>> registerDirective(""" |
|---|
| 25 |
... <xmlschema |
|---|
| 26 |
... id="sample" |
|---|
| 27 |
... document="src/zope/xmlschema/tests/examples/simple.xsd" |
|---|
| 28 |
... /> |
|---|
| 29 |
... """) |
|---|
| 30 |
|
|---|
| 31 |
Let's define an interface that will be bend to a xsd. For this, we |
|---|
| 32 |
will use the zope.xmlschema.set declaration : |
|---|
| 33 |
|
|---|
| 34 |
>>> import zope.interface |
|---|
| 35 |
>>> import zope.xmlschema |
|---|
| 36 |
>>> class ITest(zope.interface.Interface): |
|---|
| 37 |
... zope.xmlschema.set('sample') |
|---|
| 38 |
|
|---|
| 39 |
>>> type(ITest) |
|---|
| 40 |
<class 'zope.interface.interface.InterfaceClass'> |
|---|
| 41 |
|
|---|
| 42 |
>>> ITest.__name__ |
|---|
| 43 |
'ITest' |
|---|
| 44 |
>>> ITest.__doc__ |
|---|
| 45 |
'' |
|---|
| 46 |
|
|---|
| 47 |
Now introspect the fields defined within the xsd. We will see two new |
|---|
| 48 |
fields : title and description. |
|---|
| 49 |
|
|---|
| 50 |
>>> names = list(ITest) |
|---|
| 51 |
>>> names.sort() |
|---|
| 52 |
>>> names |
|---|
| 53 |
['description', 'title'] |
|---|
| 54 |
|
|---|
| 55 |
Now define a class implementing this interface. |
|---|
| 56 |
|
|---|
| 57 |
>>> class Test(object): |
|---|
| 58 |
... zope.interface.implements(ITest) |
|---|
| 59 |
... title = '' |
|---|
| 60 |
... description = '' |
|---|
| 61 |
>>> |
|---|
| 62 |
|
|---|
| 63 |
ITest is implemented by Test |
|---|
| 64 |
|
|---|
| 65 |
>>> ITest.implementedBy(Test) |
|---|
| 66 |
True |
|---|
| 67 |
|
|---|
| 68 |
Of course, Test doesn't provide "ITest", it implements it:: |
|---|
| 69 |
|
|---|
| 70 |
>>> ITest.providedBy(Test) |
|---|
| 71 |
False |
|---|
| 72 |
|
|---|
| 73 |
Let's create an instance of Test |
|---|
| 74 |
|
|---|
| 75 |
>>> test = Test() |
|---|
| 76 |
|
|---|
| 77 |
ITest is provided by test |
|---|
| 78 |
|
|---|
| 79 |
>>> ITest.providedBy(test) |
|---|
| 80 |
True |
|---|
| 81 |
|
|---|
| 82 |
Defines potential values for two fields |
|---|
| 83 |
|
|---|
| 84 |
>>> title = u"Title field" |
|---|
| 85 |
>>> description = u"Description field" |
|---|
| 86 |
|
|---|
| 87 |
Now we, get the fields from the interface: |
|---|
| 88 |
|
|---|
| 89 |
>>> title_field = ITest.get('title') |
|---|
| 90 |
>>> description_field = ITest.get('description') |
|---|
| 91 |
|
|---|
| 92 |
Next we have to bind these fields to the context, so that instance-specific |
|---|
| 93 |
information can be used for validation: |
|---|
| 94 |
|
|---|
| 95 |
>>> title_bound = title_field.bind(test) |
|---|
| 96 |
>>> description_bound = description_field.bind(test) |
|---|
| 97 |
|
|---|
| 98 |
Now try to validate them : |
|---|
| 99 |
|
|---|
| 100 |
>>> title_bound.validate(title) |
|---|
| 101 |
>>> description_bound.validate(description) |
|---|