-1

I need to write a filter to discard some elements, tags and blocks in my XML Files. In the following you can see what are my xml examples and expected outputs. I am somehow confused about the differences between element, tag, attribute in the elemetTree. My test does not work!

Filter:

import xml.etree.ElementTree as xee

def test(input):
    doc=xee.fromstring(input)
    print xee.tostring(doc)

    #RemoveTimeStampAttribute
    for elem in doc.findall('Component'):
        if 'timeStamp' in elem.attrib:
            del elem.attrib['timeStamp']

    #RemoveTimeStampElements
    for elem in doc.findall('TimeStamp'):
        del elem 
    print xee.tostring(doc) 
    return xee.tostring(doc)  

1 Answer 1

1

First of all, you are removing the attribute incorrectly, see if timeStamp is in the element's attrib dictionary and then use del to remove it:

def amdfilter(input):
    doc = xee.fromstring(input)

    for node in doc.findall('Component'):
        if 'timeStamp' in node.attrib:
            del node.attrib['timeStamp']

    return xee.tostring(doc)

Also, since you are testing only the attribute removal here, change your expectation to:

expected = '<ComponentMain><Component /></ComponentMain>'

Complete test (it passes):

import unittest
from amdfilter import *

class FilterTest(unittest.TestCase):
    def testRemoveTimeStampAttribute(self):
        input = '<?xml version="1.0"?><ComponentMain><Component timeStamp="2014"></Component></ComponentMain>'
        output = amdfilter(input)
        expected = '<ComponentMain><Component /></ComponentMain>'
        self.assertEqual(expected, output)

Note that I don't care here about the xml declaration line (it could be easily added).

Sign up to request clarification or add additional context in comments.

4 Comments

tnx, it works..just one question, is xml version tag removed when we revert the input to String?
And how to delete a node itself? Question is edited.
@OrclUser ok, TimeStamp can be anywhere in the xml, or at a specific location?
It is either an attribute or an element or a block..You can find in my examples, in the first one it is an attribute and I just need to remove that attribute, in the second one it is an element and I need to remove the whole element and in the third one the whole block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.