【问题标题】:Selecting Node with specific text that have a quote in them选择带有包含引号的特定文本的节点
【发布时间】:2013-06-08 19:38:01
【问题描述】:

我有一个 XML 文档,我在其中使用 xpath 来查找使用文本的特定节点。

例如,我要查找的产品是具有许多产品节点的文件中的以下产品节点。

<product>
    <code>10023</code>
    <name>
        <value language="en_US">There's Pippins and Cheese to Come</value>
        <value language="en_CA">There's Pippins and Cheese to Come</value>
    </name>
</product>

我正在使用 xpath 查询

node = self.productDoc.xpath("/product[name/value[text() = '{0}']]".format(self.Title))

来自 Lxml 模块的productDoc = etree.parse(FileLocation)

我的问题是:当我尝试使用上面提供的 xpath 路径选择节点时,我得到无效的谓词。 我相信问题出在“There's Pippins and Cheese to Come”文本中的单引号 这弄乱了xpath路径。 我该如何克服这个问题?

【问题讨论】:

  • 你试过没有数据中的 apos(单引号)吗? T这将是此错误的诊断。 {0} 在做什么?

标签: python xml string xpath file-io


【解决方案1】:

单引号导致问题,您需要在 xpath 中以某种方式对其进行转义或使用双引号,这应该可以代替

node = self.productDoc.xpath("/product[name/value[text() = \"{0}\"]]".format(self.Title))

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 2021-02-23
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 2019-10-29
    • 2015-05-08
    相关资源
    最近更新 更多