user1179317
user1179317

Reputation: 2923

Parse namespace from xml tag using etree

What is the best way to parse the namespace from the xml tag? For example:

<test xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
    <xsd:step name="testing"/>
</test>

Printing out the .tag using xml.etree will be:

{http://www.w3.org/2001/XMLSchema#}step

My goal is to just get the tag as "step" I know I could just remove everything in {}, but I was wondering if there is a built in command that separates the namespace and tag. If there is not a built in command, what is the best way to do this?

Upvotes: 1

Views: 61

Answers (1)

Gustavo Bezerra
Gustavo Bezerra

Reputation: 11064

It seems there is no built-in functionality in either xml or lxml to achieve what you are looking for. Just remove the portion in brackets with a regex:

import re
re.sub('{.*}', '', tag)

Upvotes: 2

Related Questions