有这样的xml文件。如何只选择该标记,哪个href属性以parent结尾,如下面的第三个元素。
根据位置来确定它,比如elem = tree.findall('{*}CustomProperty')[2]不合适,因为一些文档可能只有一个parent href,其他的可能有5-10,而第三个文档可能根本没有这样的href。
我倾向于使用xpath,但不知道如何告诉xpath搜索属性的末尾匹配。
另外,xpath不是必须的,我很乐意使用任何适合我的目的的方法。
因此,我如何获得CustomProperty href 元素,该元素具有一个以word parent 结尾的href属性
<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>预先感谢您的帮助
发布于 2015-02-13 08:24:20
尝试使用包含选择器查找具有包含单词父项的属性href的元素。
//*[contains(@href, 'parent')]或者,如果您确定文本“父”的位置,则可以使用
//*[ends-with(@href, 'parent')]发布于 2015-02-13 08:29:16
有吗?
//CustomProperty[contains(@href, 'parent') and substring-after(@href, 'parent') = '']满足你的要求?这个建议的一个问题是,对于parent不止一次发生的href属性,它失败了。
如果xpath处理器支持XPath2.0,请使用aberna的建议。
请记住,出于性能考虑,尽可能用特定路径替换“//”轴。
https://stackoverflow.com/questions/28495155
复制相似问题