如何在XPath中使用starts-with(),contains()和ends-with()来查找xml节点的innertext?(XPATH 1.0)

11
<ArticleBackmatter>
   <Heading>Ethical standards and patient consent</Heading>
   <Heading>Ethical standards and patient</Heading>
   <Heading>standards and patient consent</Heading>
</ArticleBackmatter>

我想获取标题节点中从“道德”开始,包含“和”并以“同意”结尾的纯文本。


1
你使用的XPath版本是什么(或者如果你不确定版本,使用的工具/库/语言是什么)?starts-with在XPath 1.0中可用,但是ends-with只在XPath 2.0中引入(在1.0中需要使用substringstring-length来模拟)。 - Ian Roberts
1
我只使用XPath 1.0 - Karuppa Samy
你的问题不够清晰:你的示例代码期望得到什么结果? - michael.hor257k
如果“道德标准和患者同意”是文本节点的全部内容,您是否只想要它?或者即使在它之前/之后有其他文本,您也想要该字符串? - LarsH
1个回答

29

一种可能的方法:

//Heading[starts-with(., 'Ethical') and ends-with(., 'consent')]

ends-with()函数是XPath 2.0中的函数。在XPath 1.0中,它可以使用substring()string-length()来替换。以下是等效的XPath 1.0代码(为了易读性而换行):

//Heading[
            starts-with(., 'Ethical') 
                and 
            'consent' = substring(., string-length(.) - string-length('consent') +1)
         ]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接