Skip to content

Commit

Permalink
Merge pull request #75 from XLSForm/namespaces
Browse files Browse the repository at this point in the history
add additional namespaces to XForm
  • Loading branch information
dorey authored Aug 11, 2016
2 parents 2c7421a + b4e7391 commit f91d864
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pyxform/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@

OSM = u"osm"
OSM_TYPE = u"binary"

NAMESPACES = u'namespaces'
22 changes: 21 additions & 1 deletion pyxform/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Survey(Section):
u"version": unicode,
u"choices": dict,
u"style": unicode,
u"attribute": dict
u"attribute": dict,
u"namespaces": unicode,
}
)

Expand All @@ -66,6 +67,23 @@ def _validate_uniqueness_of_section_names(self):
"There are two sections with the name %s." % e.name)
section_names.append(e.name)

def get_nsmap(self):
"""Add additional namespaces"""
namespaces = getattr(self, constants.NAMESPACES, None)

if namespaces and isinstance(namespaces, basestring):
nslist = [
ns.split('=') for ns in namespaces.split()
if len(ns.split('=')) == 2 and ns.split('=')[0] != ''
]
XMLNS = u'xmlns:'
nsmap.update(dict([
(XMLNS + k, v.replace('"', '').replace("'", ""))
for k, v in nslist if XMLNS + k not in nsmap
]))

return nsmap

def xml(self):
"""
calls necessary preparation methods, then returns the xml.
Expand All @@ -77,6 +95,8 @@ def xml(self):
self, constants.STYLE):
body_kwargs['class'] = getattr(
self, constants.STYLE)
nsmap = self.get_nsmap()

return node(u"h:html",
node(u"h:head",
node(u"h:title", self.title),
Expand Down
27 changes: 27 additions & 0 deletions pyxform/tests_v1/test_custom_xml_namespaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pyxform_test_case import PyxformTestCase


class CustomXMLNamespacesTest(PyxformTestCase):
def test_custom_xml_name_spaces(self):
# re: https://github.com/XLSForm/pyxform/issues/65
self.assertPyxformXform(
name="custom_namespaces",
md="""
| survey | | | |
| | type | name | label |
| | note | q | Q |
| settings | | | |
| | namespaces | | |
| | esri="http://esri.com/xforms" enk="http://enketo.org/xforms" naf="http://nafundi.com/xforms" | | |
""",
xml__contains=[
'xmlns="http://www.w3.org/2002/xforms"',
'xmlns:h="http://www.w3.org/1999/xhtml"',
'xmlns:jr="http://openrosa.org/javarosa"',
'xmlns:orx="http://openrosa.org/xforms"',
'xmlns:xsd="http://www.w3.org/2001/XMLSchema"',
'xmlns:esri="http://esri.com/xforms"',
'xmlns:enk="http://enketo.org/xforms"',
'xmlns:naf="http://nafundi.com/xforms"',
],
)

0 comments on commit f91d864

Please sign in to comment.