diff --git a/elementpath/datatypes.py b/elementpath/datatypes.py index ea08fe9..80ab164 100644 --- a/elementpath/datatypes.py +++ b/elementpath/datatypes.py @@ -201,7 +201,7 @@ class AbstractDateTime(object): raise ElementPathTypeError("wrong type %r for year" % type(year)) else: self._year = year - if isleap(year): + if isleap(year + bool(self.version != '1.0')): self._dt = datetime.datetime(4, month, day, hour, minute, second, microsecond, tzinfo) else: self._dt = datetime.datetime(6, month, day, hour, minute, second, microsecond, tzinfo) diff --git a/elementpath/xpath1_parser.py b/elementpath/xpath1_parser.py index 2e88afe..e8eb712 100644 --- a/elementpath/xpath1_parser.py +++ b/elementpath/xpath1_parser.py @@ -646,6 +646,8 @@ def select(self, context=None): for item in context.iter(): if item in results: yield item + elif is_attribute_node(item) and item[1] in results: + yield item[1] ### diff --git a/elementpath/xpath_context.py b/elementpath/xpath_context.py index 024ee3e..37d4874 100644 --- a/elementpath/xpath_context.py +++ b/elementpath/xpath_context.py @@ -223,9 +223,8 @@ class XPathContext(object): self.item = elem.text yield self.item - for item in elem.attrib.items(): - self.item = item - yield item + for self.item in map(lambda x: AttributeNode(*x), elem.attrib.items()): + yield self.item if len(elem): self.size = len(elem) diff --git a/elementpath/xpath_token.py b/elementpath/xpath_token.py index 605a2c0..9ecb5a7 100644 --- a/elementpath/xpath_token.py +++ b/elementpath/xpath_token.py @@ -225,7 +225,15 @@ class XPathToken(Token): try: next(selector) except StopIteration: - return str(value) if isinstance(value, UntypedAtomic) else value + if isinstance(value, UntypedAtomic): + value = str(value) + if self.xsd_type is not None and isinstance(value, string_base_type): + try: + value = self.xsd_type.decode(value) + except (TypeError, ValueError): + msg = "Type {!r} is not appropriate for the context" + self.wrong_context_type(msg.format(type(value))) + return value else: self.wrong_context_type("atomized operand is a sequence of length greater than one") diff --git a/tests/test_datatypes.py b/tests/test_datatypes.py index a5fbca2..dc7f13f 100644 --- a/tests/test_datatypes.py +++ b/tests/test_datatypes.py @@ -116,13 +116,15 @@ class DateTimeTypesTest(unittest.TestCase): self.assertRaises(TypeError, AbstractDateTime) self.assertRaises(TypeError, OrderedDateTime) - def test_init_fromstring(self): + def test_datetime_init_fromstring(self): self.assertIsInstance(DateTime.fromstring('2000-10-07T00:00:00'), DateTime) self.assertIsInstance(DateTime.fromstring('-2000-10-07T00:00:00'), DateTime) self.assertRaises(ValueError, DateTime.fromstring, '00-10-07') + def test_date_init_fromstring(self): self.assertIsInstance(Date.fromstring('2000-10-07'), Date) self.assertIsInstance(Date.fromstring('-2000-10-07'), Date) + self.assertIsInstance(Date.fromstring('0000-02-29'), Date) def test_datetime_repr(self): dt = DateTime.fromstring('2000-10-07')