Parse Xpath From Xml File Should Contain '
Solution 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:
<Itemname="Date"xpath="'p[@class="date"]/text()'"defaultValue="Date Not Found"></Item><Itemname="Date"xpath="'p[@class="date"]/text()'"defaultValue="Date Not Found"></Item><Itemname="Date"xpath=''p[@class="date"]/text()''defaultValue="Date Not Found"></Item><Itemname="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'])
Post a Comment for "Parse Xpath From Xml File Should Contain '"