Skip to content

Commit

Permalink
Merge branch 'hotfix/0.21.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rlskoeser committed Aug 28, 2014
2 parents 93a63b4 + f46616d commit 3bfdfee
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The following is a summary of changes and improvements to
:mod:`eulxml`. New features in each version should be listed, with
any necessary information about installation or upgrade notes.

0.21.1
----

* Bug fix: :meth:`eulxml.xmlmap.XmlObject.xsl_transform` now recognizes text
output as a valid, non-empty XSL result

0.21
----
Expand Down
2 changes: 1 addition & 1 deletion eulxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

__version_info__ = (0, 21, 0, None)
__version_info__ = (0, 21, 1, None)

# Dot-connect all but the last. Last is dash-connected if not None.
__version__ = '.'.join([str(i) for i in __version_info__[:-1]])
Expand Down
11 changes: 10 additions & 1 deletion eulxml/xmlmap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
:param filename: xslt filename (optional, one of file and xsl is required)
:param xsl: xslt as string (optional)
:param return_type: type of object to return; optional, defaults to
:class:`XmlObject`
:class:`XmlObject`; specify unicode or string for text output
:returns: an instance of :class:`XmlObject` or the return_type specified
"""
parser = _get_xmlparser()
Expand All @@ -381,6 +381,15 @@ def xsl_transform(self, filename=None, xsl=None, return_type=None, **params):
# 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.

# text output does not include a root node, so check separately
if return_type in [str, unicode]:
if result is None:
logger.warning("XSL transform generated an empty result")
return
else:
return return_type(result)

if result is None or result.getroot() is None:
logger.warning("XSL transform generated an empty result")
else:
Expand Down
19 changes: 19 additions & 0 deletions test/test_xmlmap/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ class TestXsl(unittest.TestCase):
</xsl:stylesheet>
'''

TEXT_OUTPUT_XSL = '''
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements="foo bar"/>
<xsl:template match="baz">
<xsl:apply-templates/><xsl:text> </xsl:text>
</xsl:template>
</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 Down Expand Up @@ -101,6 +111,15 @@ class TestObject(xmlmap.XmlObject):
return_type=TestObject),
'xsl transform should return None for an empty XSLT result')

# text output
obj = TestObject(self.fixture)
result = obj.xsl_transform(xsl=self.TEXT_OUTPUT_XSL, return_type=str)
self.assertEqual('42 13 ', result)
self.assert_(isinstance(result, str))

result = obj.xsl_transform(xsl=self.TEXT_OUTPUT_XSL, return_type=unicode)
self.assert_(isinstance(result, unicode))

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

Expand Down

0 comments on commit 3bfdfee

Please sign in to comment.