Invalid tag name error when creating element with lxml in python

2024/9/17 3:08:05

I am using lxml to make an xml file and my sample program is :

from lxml import etree
import datetime
dt=datetime.datetime(2013,11,30,4,5,6)
dt=dt.strftime('%Y-%m-%d')
page=etree.Element('html')
doc=etree.ElementTree(page)
dateElm=etree.SubElement(page,dt)
outfile=open('somefile.xml','w')
doc.write(outfile)

And I am getting the following error output :

dateElm=etree.SubElement(page,dt)File "lxml.etree.pyx", line 2899, in lxml.etree.SubElement (src/lxml/lxml.etree.c:62284)File "apihelpers.pxi", line 171, in lxml.etree._makeSubElement (src/lxml/lxml.etree.c:14296)File "apihelpers.pxi", line 1523, in lxml.etree._tagValidOrRaise (src/lxml/lxml.etree.c:26852)
ValueError: Invalid tag name u'2013-11-30'

I thought it of a Unicode Error, so tried changing encoding of 'dt' with codes like

  1. str(dt)
  2. unicode(dt).encode('unicode_escape')
  3. dt.encocde('ascii','ignore')
  4. dt.encode('ascii','decode')

and some others also, but none worked and same error msg generated.

Answer

You get the error because element names are not allowed to begin with a digit in XML. See http://www.w3.org/TR/xml/#sec-common-syn and http://www.w3.org/TR/xml/#sec-starttags. The first character of a name must be a NameStartChar, which disallows digits.

An element such as <2013-11-30>...</2013-11-30> is invalid.

An element such as <D2013-11-30>...</D2013-11-30> is OK.

If your program is changed to use ElementTree instead of lxml (from xml.etree import ElementTree as etree instead of from lxml import etree), there is no error. But I would consider that a bug. lxml does the right thing, ElementTree does not.

https://en.xdnf.cn/q/73294.html

Related Q&A

Method replacement at runtime not updating Private attributes

I understood how to replace methods at run time in Python by going through these links.[ Link1 , Link2 , & Link3].When I replaced a "update_private_variable" method of class A, its gettin…

sys.path and sys.executable is incorrect in jupyter, but no applied fix is working

Ive configured jupyter to be used from a remote computer and set a password to it while initial anaconda setup. Then after fixing this issue, I am trapped in another one. sys.path and sys.executable is…

How to multicolour text with ScrolledText widget?

from tkinter import * from tkinter.scrolledtext import ScrolledTextwindow= Tk() window.geometry(970x45) box = ScrolledText(window, width=70, height=7).pack() box.insert(END, "Ehila") #this in…

Binding Return to button is not working as expected

I bound the event <Return> to a Button, thinking that this would cause the command to be run after hitting Enter:Button(self.f, text="Print", command=self.Printer).pack(side=RIGHT, padx…

ZMQ Pub-Sub Program Failure When Losing Network Connectivity

I have a simple pub-sub setup on a mid-sized network, using ZMQ 2.1. Although some subscribers are using C# bindings, others are using Python bindings, and the issue Im having is the same for either.I…

replacing html tags with BeautifulSoup

Im currently reformatting some HTML pages with BeautifulSoup, and I ran into bit of a problem.My problem is that the original HTML has things like this:<li><p>stff</p></li>and &…

LightGBM: train() vs update() vs refit()

Im implementing LightGBM (Python) into a continuous learning pipeline. My goal is to train an initial model and update the model (e.g. every day) with newly available data. Most examples load an alread…

GTK: create a colored regular button

How do I do it? A lot of sites say I can just call .modify_bg() on the button, but that doesnt do anything. Im able to add an EventBox to the button, and add a label to that, and then change its color…

How to Normalize similarity measures from Wordnet

I am trying to calculate semantic similarity between two words. I am using Wordnet-based similarity measures i.e Resnik measure(RES), Lin measure(LIN), Jiang and Conrath measure(JNC) and Banerjee and P…

How to open chrome developer console using Selenium in Python?

I am trying to open developer console in chrome using selenium webdriver. I am doingfrom selenium import webdriverfrom selenium.webdriver.common import action_chains, keys...browser = webdriver.Chrome(…