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

I want to get the inner text with start from "Ethical" , contains "and " and end with "consent" in the Heading node.

Karuppa Samy
  • 161
  • 1
  • 1
  • 9
  • 1
    What version of XPath (or what tool/library/language if you don't know the version for sure)? `starts-with` is available in XPath 1.0 but `ends-with` was only introduced by XPath 2.0 (you have to fake it with `substring` and `string-length` in 1.0). – Ian Roberts Oct 06 '14 at 13:23
  • 1
    I use XPath 1.0 only – Karuppa Samy Oct 06 '14 at 13:27
  • Your question is not clear: what is the expected result of your example? – michael.hor257k Oct 06 '14 at 13:31
  • Do you only want `Ethical standards and patient consent` if that is the entirety of the content of a text node? Or do you want that string even if there is other text before/after it? – LarsH Oct 06 '14 at 13:50

1 Answers1

29

One possible way:

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

The ends-with() function is XPath 2.0. In XPath 1.0, it can be replaced using substring() and string-length(). Here is the equivalent XPath 1.0 (wrapped for readability):

//Heading[
            starts-with(., 'Ethical') 
                and 
            'consent' = substring(., string-length(.) - string-length('consent') +1)
         ]
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
har07
  • 88,338
  • 12
  • 84
  • 137