使用Python查找和转换XML处理指令

2024-09-27 22:09:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我们正在将我们古老的FrameMaker文档转换为XML。我的工作是转换:

<?FM MARKER [Index] foo, bar ?>` 

为此:

^{pr2}$

我不担心这一部分;困扰我的是ProcessingInstruction遍布文档,并且可能位于任何元素下,所以我需要能够搜索整个树,找到它们,然后处理它们。我不知道如何使用minidom遍历整个XML树。我缺少一些秘密方法/迭代器吗?到目前为止,我看到的是:

  • Elementtree具有优秀的Element.iter()方法,这是一种深度优先搜索,但它不处理ProcessingInstructions。

  • ProcessingInstruction没有标记名,因此我无法使用minidomgetElementsByTagName来搜索它们。

  • xml.saxContentHandler.processingInstruction看起来只用于创建ProcessingInstructions。

除了创建我自己的深度优先搜索算法,有没有办法在XML文件中生成ProcessingInstruction的列表,或者标识它们的父级?在


Tags: 方法文档元素indexfoobarxmlmarker
1条回答
网友
1楼 · 发布于 2024-09-27 22:09:27

使用lxml模块的XPath API:

from lxml import etree

foo = StringIO('<foo><bar></bar></foo>')
tree = etree.parse(foo)
result = tree.xpath('//processing-instruction()')

The node test processing-instruction() is true for any processing instruction. The processing-instruction() test may have an argument that is Literal; in this case, it is true for any processing instruction that has a name equal to the value of the Literal.

参考文献

相关问题 更多 >

    热门问题