有这样的xml文件。 如何只选择那个href属性以parent结尾的标记,如下面的第三个元素。
按位置确定它,如elem = tree.findall('{*}CustomProperty')[2]不适合,因为某些文档可能只有一个parent href,其他5-10和第三可能根本没有这样的hrefs。
我倾向于使用xpath,但无法弄清楚如何告诉xpath搜索属性匹配的结束。
xpath也不是必须的,我很乐意使用任何符合我目的的方式
那么我怎样才能获得具有以word parent结尾的href属性的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">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>提前感谢您的帮助
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
最满意答案
尝试使用contains选择器查找具有属性href的元素,该属性包含单词parent
//*[contains(@href, 'parent')]或者如果你确定文本“parent”的位置,你可以使用ends-with
//*[ends-with(@href, 'parent')]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')]更多推荐
发布评论