从 xml 文件解析 xpath 应包含 ' [英] parse xpath from xml file should contain '

查看:71
本文介绍了从 xml 文件解析 xpath 应包含 '的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml文件

<Item name="Date" xpath='p[@class="date"]/text()' defaultValue="Date Not Found"></Item>

我是这样解析的:

self.doc=etree.parse(xmlFile)
masterItemsFromXML = self.doc.findall('MasterPage/MasterItems/Item')
        for oneItem in masterItemsFromXML:
            print 'master item xpath = {0}'.format(oneItem.attrib['xpath'])

并且我可以在 cmd 中看到打印的结果,如下所示:

and I can see the result printed in the cmd like this:

master item xpath =p[@class="date"]/text()

我的问题

xpath 无效,因为它应该以 ' 开头并以 ' 结束

我试过了

name="Date" xpath='''p[@class="date"]/text()'''

但后来我在解析 xml 时出错.

but then i got error in parsing the xml.

帮助

推荐答案

在 XML 中,属性值总是用单引号或双引号引起来.有关详细信息,请参阅规范.这些引号不是属性值的一部分.所以,正如所写的,你的属性值是 p[@class="date"]/text()——正是你从代码中得到的.

In XML, attribute values are always quoted with single or double quotes. See the spec for details. Those quotes are not part of the attribute value. So, as written, your attribute value is p[@class="date"]/text()—exactly what you're getting from your code.

那么,如果您想在实际值中同时使用单引号和双引号怎么办?好吧,如果你单引号的值,里面不能有单引号;如果你双引号它,里面不能有双引号;并且没有其他选择.

So, what if you want to have both single and double quotes in the actual value? Well, if you single-quote the value, it can't have single quotes inside; if you double-quote it, it can't have double-quotes inside; and there are no other options.

Python 有一个很好的解决方案,将文字周围的引号增加三倍,但这只是 Python.其他语言有不同的解决方案,例如将文字中间的引号加倍,或使用反斜杠转义.

Python has a nice solution for that, tripling the quotes around the literal, but that's only Python. Other languages have different solutions, like doubling the quotes in the middle of the literal, or using backslash escapes.

XML 具有的是实体引用和字符引用.所以,这些都将是您想要的:

What XML has is entity reference and character references. So, any of these will be what you want:

<Item name="Date" xpath="'p[@class=&quot;date&quot;]/text()'" defaultValue="Date Not Found"></Item>

<Item name="Date" xpath="'p[@class=&#34;date&#34;]/text()'" defaultValue="Date Not Found"></Item>

<Item name="Date" xpath='&apos;p[@class="date"]/text()&apos;' defaultValue="Date Not Found"></Item>

<Item name="Date" xpath='&#39;p[@class="date"]/text()&#39;' defaultValue="Date Not Found"></Item>

现在您有一个正确引用的属性值,其中包含单引号.

Now you have a properly-quoted attribute value that contains single quotes within it.

说了这么多,你确定你真的想要你的 xpath 值中的那些单引号吗?毕竟,没有这些引号,它就是一个有效的 XPath 表达式;与他们,它不是.如果您只想在有效值周围打印引号,而不是将它们嵌入到值中,那就更简单了:

All that being said, are you sure you actually want those single quotes in your xpath value? After all, without those quotes, it's a valid XPath expression; with them, it's not. If all you want to do is print quotes around the valid, not embed them into the value, that's even easier:

print "master item xpath = '{0}'".format(oneItem.attrib['xpath'])

这篇关于从 xml 文件解析 xpath 应包含 '的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆