From d806184bb6c13b0d0b121a8a5a29260508d4cd43 Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Tue, 8 Oct 2013 00:01:44 +0200 Subject: [PATCH] .remove_namespaces(): remove namespaces on elements' attributes --- scrapy/selector/lxmlsel.py | 4 ++++ scrapy/tests/test_selector_lxml.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/scrapy/selector/lxmlsel.py b/scrapy/selector/lxmlsel.py index 8708144bd..5bf9fd1dc 100644 --- a/scrapy/selector/lxmlsel.py +++ b/scrapy/selector/lxmlsel.py @@ -77,6 +77,10 @@ class XPathSelector(object_ref): for el in self._root.iter('*'): if el.tag.startswith('{'): el.tag = el.tag.split('}', 1)[1] + # loop on element attributes also + for an in el.attrib.keys(): + if an.startswith('{'): + el.attrib[an.split('}', 1)[1]] = el.attrib.pop(an) def __nonzero__(self): return bool(self.extract()) diff --git a/scrapy/tests/test_selector_lxml.py b/scrapy/tests/test_selector_lxml.py index f9c9ec2ac..352861ae6 100644 --- a/scrapy/tests/test_selector_lxml.py +++ b/scrapy/tests/test_selector_lxml.py @@ -27,6 +27,18 @@ class LxmlXPathSelectorTestCase(test_selector.XPathSelectorTestCase): xxs.remove_namespaces() self.assertEqual(len(xxs.select("//link")), 2) + def test_remove_attributes_namespaces(self): + xml = """ + + + + +""" + xxs = XmlXPathSelector(XmlResponse("http://example.com/feed.atom", body=xml)) + self.assertEqual(len(xxs.select("//link/@type")), 0) + xxs.remove_namespaces() + self.assertEqual(len(xxs.select("//link/@type")), 2) + class Libxml2DocumentTest(unittest.TestCase): def test_caching(self):