# HG changeset patch # User hodgestar # Date 1395320328 0 # Node ID a21009a2bc3ab303054176f797dd99ea57914f68 # Parent accc8a0cf48658c495d812c0320f17055ed128ee Return correct value and properly namespaced attribute name when matching namespaced attributes with XPath expressions (fixes #572; thanks to Olemis Lang for bug report and suggestion for fix). diff --git a/genshi/path.py b/genshi/path.py --- a/genshi/path.py +++ b/genshi/path.py @@ -1008,7 +1008,7 @@ qname = QName('%s}%s' % (namespaces.get(self.prefix), self.name)) if kind is START: if self.principal_type is ATTRIBUTE and qname in data[1]: - return Attrs([(self.name, data[1].get(self.name))]) + return Attrs([(qname, data[1].get(qname))]) else: return data[0] == qname def __repr__(self): diff --git a/genshi/tests/path.py b/genshi/tests/path.py --- a/genshi/tests/path.py +++ b/genshi/tests/path.py @@ -14,6 +14,7 @@ import doctest import unittest +from genshi.core import Attrs, QName from genshi.input import XML from genshi.path import Path, PathParser, PathSyntaxError, GenericStrategy, \ SingleStepStrategy, SimplePathStrategy @@ -630,6 +631,25 @@ '/descendant::b/descendant::c', input=xml, output='') + def test_attr_selection(self): + xml = XML('') + path = Path('foo/@bar') + result = path.select(xml) + self.assertEqual(list(result), [ + Attrs([(QName('bar'), u'abc')]) + ]) + + def test_attr_selection_with_namespace(self): + xml = XML( + '' + '' + '') + path = Path('foo/@ns1:bar') + result = path.select(xml, namespaces={'ns1': 'http://example.com'}) + self.assertEqual(list(result), [ + Attrs([(QName('http://example.com}bar'), u'abc')]) + ]) + def _test_support(self, strategy_class, text): path = PathParser(text, None, -1).parse()[0] return strategy_class.supports(path)