0
lxml.sax
を使用してXMLドキュメントを変更しています。カスタム属性をstartElementNS
メソッドに渡したいと思います。私の最初のコードはこれです:lxml.saxインターフェイスを使用してカスタム属性を渡すにはどうすればよいですか?
from lxml import sax
class MyContentHandler(sax.ElementTreeContentHandler):
def startElementNS(self, ns_name, qname, attributes):
if qname == 'text':
# the troublesome line
super().startElementNS((None, 'div'), 'div', {'class':'page'})
super().startElementNS(ns_name, qname, attributes)
しかし、documentationノートとして、attributes
パラメータがAttributes
オブジェクトではなく、通常の辞書にする必要があります。デバッグでは、正確なタイプがsax.xmlreader.AttributesNSImpl
であることが明らかになりましたが、このクラスは正式には文書化されておらず、直接インスタンス化できないようです。
startElementNS
メソッドに独自の属性を指定する方法はありますか。試行錯誤のより