0

すべてのテキスト ノードについて、親要素のクラスとタグ タイプを見つけるにはどうすればよいですか

4

1 に答える 1

1

XPath ドキュメントから、それほど難しくありません。これが私の意図的な XML ファイルです。

<root>
    <child1>
        <text>Text1</text>
    </child1>
    <child2>
        <text>Text2</text>
    </child2>
    <child3>
        <text>Text3</text>
    </child3>
    <child4>
        <text>Text4</text>
    </child4>
</root>

XPath サポートを実装するlxml ライブラリ(組み込みの Python XML ライブラリには当てはまりません) を使用すると、次のようになります。

>>> from lxml import etree
>>> root = etree.parse(path).getroot()
>>> for p in root.xpath('//text/..'):
    print p.tag


child1
child2
child3
child4
于 2013-02-06T18:09:05.470 に答える