parse xpath from xml file should contain '

烂漫一生 提交于 2021-02-11 15:55:49

问题


this is my xml file

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

i parse it like this:

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'])

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

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

my problem

the xpath is not valid because it should start with ' and end with '

what I tried

i tried this

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

but then i got error in parsing the xml.

help


回答1:


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

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.


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'])



回答2:


Use an element:

<Item ...>
  <xpath>p[@class="date"]/text()</xpath>
</Item>


来源:https://stackoverflow.com/questions/21765139/parse-xpath-from-xml-file-should-contain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!