繁体   English   中英

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

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

有这样的xml文件。 我如何只选择该标签,该标签的href属性以parent结尾,如下面的第三个元素。

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

我倾向于使用xpath,但无法弄清楚如何告诉xpath搜索属性匹配的结尾。

而且xpath不是必须的,我将很高兴使用适合我目的的任何方法

那么,如何获取具有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>

预先感谢您的帮助

是否

//CustomProperty[contains(@href, 'parent') and substring-after(@href, 'parent') = '']

满足您的要求? 该建议的一个问题是,对于parent属性多次出现的href属性,它会失败。

如果您的xpath处理器支持xpath 2.0,请使用aberna的建议。

请记住,出于性能原因,请尽可能用特定路径替换“ //”轴。

尝试使用contains选择器来查找具有href属性的元素,该元素包含单词parent

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

或者,如果您确定“父母”文本的位置,则可以使用-

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

暂无
暂无

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

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