0

I have an XML document where i am using xpath to find specific nodes using the text.

For example the product that i am trying to find is the following product node in a file that has many product nodes.

<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>

I am using the xpath query

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

where productDoc = etree.parse(FileLocation) from the Lxml module

My question is: when i try to select node with the xpath path provided above, i get invalid Predicate. I believe the problem is the single quote in the "There's Pippins and Cheese to Come" text which is messing with the xpath path. How do I overcome this?

4

1 回答 1

0

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

node = self.productDoc.xpath("/product[name/value[text() = \"{0}\"]]".format(self.Title))
于 2013-06-12T16:04:18.990 回答