5

I am having trouble getting an XPath function to work. I am supposed to pull only the entries in the XML file that the name of them ends with the letter 'z' and I cannot get it to work. I am xmllint to manipulate the file with the cat command.

ends-with() only works on XPath 2.0 and I'm pretty sure the server only has XPath 1.0 so I have tried entering this command:

cat //country/city[substring(@name, string-length(@name) - string-length('z') +1)]/name

but I keep getting an error, and I am not sure if I am writing it improperly or if it is not the correct way to do it.

BMitrano825
  • 123
  • 1
  • 10

2 Answers2

3

This should work:

//country[substring(@name, string-length(@name)) = 'z']
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • I need to use a normalize space i think. Any idea how to format that around the substring function? – BMitrano825 Feb 15 '16 at 01:45
  • @BMitrano825 Use it on all `@name` within `substring()` like `substring(normalize-space(@name), string-length(normalize-space(@name)))` – har07 Feb 15 '16 at 01:50
3

Your XPath is missing the step of checking return value of substring() whether or not it is equals to the expected string ending value "z". So the correct XPath would be :

//country[substring(@name, string-length(@name) - string-length('z') +1) = 'z']/@name

And since -string-length('z') +1 equals to 0, that part can be omitted to get a simpler XPath, similar to the one posted by @Kirill Polishchuk in the other answer.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137
  • This was it pretty much. I had to make a few changes but here was my final answer i got. cat //country/city[substring(normalize-space(name), string-length(normalize-space(name)) – string-length(‘z’)+1) = ‘z’]/name – BMitrano825 Feb 15 '16 at 04:39