简体   繁体   English

通过xpath选择,仅知道元素属性的结尾

[英]Select by xpath knowing only ending of element's attribute

Having such xml file. 有这样的xml文件。 How can I select only that tag, which href attribute ends with parent , like third element below. 我如何只选择该标签,该标签的href属性以parent结尾,如下面的第三个元素。

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. 通过elem = tree.findall('{*}CustomProperty')[2]之类的位置来确定它不适合,因为某些文档可能只有一个parent href,而其他5-10和第三个文档可能根本没有此类href。

I tend to use xpath but can not figure out how can I tell xpath to search for end of attribute match. 我倾向于使用xpath,但无法弄清楚如何告诉xpath搜索属性匹配的结尾。

Also xpath is not must, I will be glad to use any way that fits to my purpose 而且xpath不是必须的,我将很高兴使用适合我目的的任何方法

So how can I get CustomProperty element which has a href attribute that ends with word parent ? 那么,如何获取具有href属性以单词parent结尾的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>

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. 该建议的一个问题是,对于parent属性多次出现的href属性,它会失败。

If your xpath processor supports xpath 2.0, use aberna's suggestion. 如果您的xpath处理器支持xpath 2.0,请使用aberna的建议。

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

//*[contains(@href, 'parent')]

or if you are sure about the position of text "parent" you can use the ends-with 或者,如果您确定“父母”文本的位置,则可以使用-

//*[ends-with(@href, 'parent')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM