{"id":21,"date":"2015-02-02T07:50:19","date_gmt":"2015-02-02T07:50:19","guid":{"rendered":"http:\/\/bendustries.org\/wp\/?p=21"},"modified":"2015-02-02T07:50:19","modified_gmt":"2015-02-02T07:50:19","slug":"setting-xmllang-with-lxml","status":"publish","type":"post","link":"https:\/\/bendustries.org\/wp\/?p=21","title":{"rendered":"Setting xml:lang with lxml"},"content":{"rendered":"<p>Unlike every other attribute I&#8217;ve run across, setting <code>xml:lang<\/code> kept failing with an &#8216;invalid attribute name&#8217; error. After the jump is how I fixed it.<br \/>\n<!--more--><br \/>\nPython and lxml make things easy. Really easy. So I was a bit lost in the woods when it kept throwing an &#8216;invalid attribute name&#8217; error when I would try to set the <code>xml:lang<\/code> attribute. After a bit of digging, I found <a href=\"http:\/\/ditanauts.org\/2012\/05\/04\/python-lxml-and-setting-xmllang\/\" title=\"this post\">This post<\/a> on <a href=\"http:\/\/ditanauts.org\" title=\"Ditanauts\" target=\"_blank\">Ditanauts.org<\/a>.<\/p>\n<p>Turns out setting <code>xml:lang<\/code> is a little special. Where I begin to write the xml out looks like this:<\/p>\n<p><code>topline = ET.Element('cXML')<br \/>\ntopline.set(\"timestamp\", timestamp)\t# set topline tags<br \/>\ntopline.set(\"payloadID\", payload)<\/code><\/p>\n<p>Now where I was trying the same, eg <code>topline.set(\"xml:lang\", \"en-US\")<\/code>, that wasn&#8217;t happening. Instead I had to do this, as Ditanauts makes clear:<br \/>\n<code>attr = topline.attrib<br \/>\nattr['{http:\/\/www.w3.org\/XML\/1998\/namespace}lang'] = \"en-US\"<\/code><\/p>\n<p>Works like a charm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike every other attribute I&#8217;ve run across, setting xml:lang kept failing with an &#8216;invalid attribute name&#8217; error. After the jump is how I fixed it.<\/p><p><a class=\"more-link block-button\" href=\"https:\/\/bendustries.org\/wp\/?p=21\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,18,19],"tags":[23,24,22,21],"class_list":["post-21","post","type-post","status-publish","format-standard","hentry","category-cxml","category-programming","category-python","tag-cxml","tag-lxml","tag-programming","tag-python"],"_links":{"self":[{"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/21","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21"}],"version-history":[{"count":1,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=\/wp\/v2\/posts\/21\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bendustries.org\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}