user1552294
user1552294

Reputation: 125

Detecting incorrect nesting of XML tags in Python

A question about testing proper nesting of XML tags:

I got a list of tags, extracted from top to bottom from an xml file:

  1. Closing tags are clearly indicated by forward slash
  2. /to and /lastname tags are incorrectly nested. They should be switched. /lastname should be within to, /to parent tags.

tag_list = ['note', 'to', 'firstname', '/firstname', 'lastname', '/to', '/lastname', '/note']

What would be the code or direction to spot that /lastname tag is outside of its parent which is to, /to pair?

Cheers.

Upvotes: 0

Views: 92

Answers (2)

Don Roby
Don Roby

Reputation: 41135

Make an empty stack.

  • Iterating through the list:
    • if you find a start tag, push it onto the stack.
    • if you find an end tag, compare it to the entry on top of the stack.
      • if the stack is empty or the top doesn't match, fail.
      • if it matches, pop the stack and continue.
  • At the end of the iteration:
    • if the stack is empty, declare success.
    • otherwise fail.

Upvotes: 1

Ami Tavory
Ami Tavory

Reputation: 76346

Remove the backslashes, iterate on the reversed version, and compare with the original.

E.g., this will give you the indices of discrepancy:

wo = [tag[1: ] if tag and tag[0] == '/' else tag for tag in taglist]
rev = list(reversed(wo))
discrepancies = [i for i in xrange(len(wo)) if wo[i] != rev[i]]

Upvotes: 2

Related Questions