Skip to content

Commit

Permalink
handle xsl transform that returns an empty result
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Jan 28, 2013
1 parent 941fb28 commit d93e2c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ any necessary information about installation or upgrade notes.

* Corrected a minor bug where schema validation errors were not cleared between
multiple validations.
* To avoid permission denied warning for auto-generated parser files,
parsetab files are now created in python tempdir if the running user
doesn't have write permission in the package installation directory.
[`Issue 1 <https://github.com/emory-libraries/eulxml/issues/1>`_]
* When an XSLT transformation results in an empty document,
:meth:`eulxml.xmlap.XmlObject.xsl_transform` now returns None.
[`Issue 6 <https://github.com/emory-libraries/eulxml/issues/6>`_]
*


0.18.0 - Formset Ordering and DateTime
Expand Down
16 changes: 12 additions & 4 deletions eulxml/xmlmap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
from lxml import etree
from lxml.builder import ElementMaker

from eulxml.xmlmap.fields import Field, NodeList
from eulxml.xmlmap.fields import Field

logger = logging.getLogger(__name__)

__all__ = [ 'XmlObject', 'parseUri', 'parseString', 'loadSchema',
'load_xmlobject_from_string', 'load_xmlobject_from_file' ]
__all__ = ['XmlObject', 'parseUri', 'parseString', 'loadSchema',
'load_xmlobject_from_string', 'load_xmlobject_from_file']

# NB: When parsing XML in this module, we explicitly create a new parser
# each time. Without this, lxml 2.2.7 uses a global default parser. When
Expand Down Expand Up @@ -374,7 +374,15 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
# - to output xml result, use serialize instead of unicode
if return_type is None:
return_type = XmlObject
return return_type(transform(self.node))

result = transform(self.node)
# If XSLT returns nothing, transform returns an _XSLTResultTree
# with no root node. Log a warning, and don't generate an
# empty xmlobject which will behave unexpectedly.
if result is None or result.getroot() is None:
logger.warning("XSL transform generated an empty result")
else:
return return_type(result)

def __unicode__(self):
if isinstance(self.node, basestring):
Expand Down
12 changes: 12 additions & 0 deletions test/test_xmlmap/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ class TestXsl(unittest.TestCase):
</xsl:stylesheet>
'''

EMPTY_RESULT_XSL = '''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/" />
</xsl:stylesheet>
'''

def setUp(self):
# parseString wants a url. let's give it a proper one.
url = '%s#%s.%s' % (__file__, self.__class__.__name__, 'FIXTURE_TEXT')
Expand All @@ -89,6 +95,12 @@ class TestObject(xmlmap.XmlObject):
self.assertEqual('42', newobj.nobar_baz)
self.assertEqual(None, newobj.bar_baz)

# empty result
obj = TestObject(self.fixture)
self.assertEqual(None, obj.xsl_transform(xsl=self.EMPTY_RESULT_XSL,
return_type=TestObject),
'xsl transform should return None for an empty XSLT result')

self.FILE.close()
# not yet tested: xsl with parameters

Expand Down

0 comments on commit d93e2c4

Please sign in to comment.