问题
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="date"]/text()'" defaultValue="Date Not Found"></Item>
<Item name="Date" xpath="'p[@class="date"]/text()'" defaultValue="Date Not Found"></Item>
<Item name="Date" xpath=''p[@class="date"]/text()'' defaultValue="Date Not Found"></Item>
<Item name="Date" xpath=''p[@class="date"]/text()'' 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