Having such xml file. How can I select only that tag, which href
attribute ends with parent
, like third element below.
Determine it by position like elem = tree.findall('{*}CustomProperty')[2]
does not fit because some documents might have only one parent
href, others 5-10 and third might not have such hrefs at all.
I tend to use xpath but can not figure out how can I tell xpath to search for end of attribute match.
Also xpath is not must, I will be glad to use any way that fits to my purpose
So how can I get CustomProperty
element which has a href
attribute that ends with word parent
?
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
<CustomProperty href="urn:1653267:643562dafewq:cs:46wey5ge:234566:parent">urn:1653267:643562dafewq:cs:46wey5ge:234566:ss</CustomProperty>
Thank you in advance for help
Does
//CustomProperty[contains(@href, 'parent') and substring-after(@href, 'parent') = '']
cater to your requirements? One issue with the suggestion is that it fails for href attributes where parent
occurs more than once.
If your xpath processor supports xpath 2.0, use aberna's suggestion.
Remember to replace the '//' axis by specific paths whereever possible for performance reasons.
Try using the contains selector to find the element with an attribute href which contains the word parent
//*[contains(@href, 'parent')]
or if you are sure about the position of text "parent" you can use the ends-with
//*[ends-with(@href, 'parent')]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.