をお願いします。
次のような自己終了タグを含む、django を使用して RSS フィードを作成するには
<tag key="value"/>
現在のセットアップ
django を使用して RSS フィードを作成していSimplerXMLGenerator
ます。XMLGenerator
これは基本的に、1 つの便利な関数が追加された単なる pythonです。
"""
Utilities for XML generation/parsing.
"""
from xml.sax.saxutils import XMLGenerator
class SimplerXMLGenerator(XMLGenerator):
def addQuickElement(self, name, contents=None, attrs=None):
"Convenience method for adding an element with no children"
if attrs is None: attrs = {}
self.startElement(name, attrs)
if contents is not None:
self.characters(contents)
self.endElement(name)
class xml.sax.saxutils.XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)
このクラスは、SAX イベントを XML ドキュメントに書き戻すことによってContentHandlerインターフェースを実装します。
「[python] バージョン 3.2 の新機能: short_empty_elements パラメータ。」
の場合short_empty_elements=True
、属性はあるがコンテンツがないタグの出力は、
<tag attr="value"/>
このように開始タグと終了タグを分離するのではなく、短い自己終了タグになります。
<tag attr="value"></tag>
ただし、私はpython 2.7を使用しているため、設定するオプションがありませんshort_empty_elements=True
(そのキーワードは認識されません)
具体的には、django のRss201rev2Feed (それ自体がSyndicationFeedのサブクラス) を使用してフィードを生成しています。
タグはContentHandlerを介して追加されます
handler = SimplerXMLGenerator(outfile, encoding)
現在、「空の」タグを追加すると
handler.addQuickElement("tag", "", {"attr": "value"})
結果は
<tag attr="value"></tag>
一方、私はしたいです
<tag attr="value"/>
Python 2.7で、これらの短い空の自己終了タグをレンダリングするようにフィードを設定できる方法を知っている人はいますか?