bulkmoustache
bulkmoustache

Reputation: 2025

Find tag text in xml

I have the following xml:

<a:something>text-a</a:something>
  <a:otherthing>text-b</a:otherthing>

and I want to assign a variable with the text of <a:otherthing>.

I tried txt = xml.find("a:otherthing").text but it shows me SyntaxError: prefix 'a' not found in prefix map

how do I do this?

Upvotes: 0

Views: 81

Answers (1)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44092

Your XML shall somewhere above declare namespace for given prefix "a".

Note, that XML allows changing purpose of namespace few times in one document (but this is not used often).

Then you will find, that for "ns:a" there is something line "http://a.alfa.aa/a/aaa.aa" string, which is so called fully qualified namespace.

In your find you shall then use a namespace map in form of

nsmap = {"a": "http://a.alfa.aa/a/aaa.aa"}
xml.find("a:otherthing", namespaces=nsmap)

Upvotes: 1

Related Questions